使用virtualenv -p /usr/local/bin/python3.4 py34env
安装python3环境报错,缺少zlib
可在python中导入zlib
缺没有报错,错误如下:
[root@localhost crazycabbage]# virtualenv -p /usr/local/bin/python3.4 py34env
Running virtualenv with interpreter /usr/local/bin/python3.4
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/virtualenv.py", line 22, in <module>
import zlib
ImportError: No module named 'zlib'
[root@localhost crazycabbage]# python
Python 2.7.5 (default, Nov 20 2015, 02:00:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zlib
>>>
Google后说在编译时加上--with-zlib
,可报错 unrecognized options: --with-zlib
,求大神帮忙!
CentOS-7
is installed by default withPython2.7.5
, as you enter after enteringPython
undershell
Written in the interpreter:Python 2.7.5 (default, Nov 20 2015, 02:00:19).
CentOS-7
默认安装的是Python2.7.5
,如你在shell
下输入Python
后进入的解释器里写的:Python 2.7.5 (default, Nov 20 2015, 02:00:19)。
Python3.4
应该是你自己装的,之所以2.7.5
下可以import zlib
,而3.4
下不能,可能是因为你的3.4
的库中没有zlib
这个库,所以你需要先确认一下3.4
到底有没有zlib
。终端输入
/usr/local/bin/python3.4
进入3.4
的解释器,然后输入import zlib
看看有没有报ImportError
异常,如果有,继续看下面的内容,如果一切正常,直接给我留言……你需要尝试重新编译安装一遍对应
Python
版本的zlib
,以确保正确的zlib
被安装,甚至我建议你把zlib-devel
也安装了。但很多时候重新安装并不管用,要么会在使用
Yum
等包管理器安装时,在安装过程中看到类似“已经存在”、“已安装”(当然是英文提示)的字样,要么强行安装后依然无法使用。这是因为它仍然安装到了
Python2.7
的库目录下,解决方法如下:在
Python2.7.5
的解释器中,输入如下代码,查看2.7的库目录:输出结果:
最直接的方法是把所需的目录添加到Python3.4的系统路径中。
首先把你的系统默认Python改为Python3.4,代替Python2.7.5:
终端中输入
Python
进入解释器验证。然后运行
vim ~/.bashrc
编辑系统环境变量,进入编辑模式后再该文件最后加上这一句export PYTHONPATH=$PYTHONPATH:/usr/lib64/python2.7/xxx/:/usr/lib64/python2.7/xxx/
,xxx是你需要的库目录,冒号分割,然后终端输入source ~/.bashrc
让其在当前终端有效,或者直接重启设备。你可能会问我,我怎么知道
zlib
在哪个目录?呃,好吧,帮人帮到底……进入
Python3.4
解释器,把上面输出的2.7
的目录逐个append
到sys.path
这个列表里,然后import zlib
试过去,不报错为止,办法虽土,行之有效。导入整个目录虽然直接且方便,但很可能会和3.4的库有冲突,所以你最好能知道
zlib
是哪些文件,然后copy
到3.4
的系统路径下,譬如_sqlite3
这个东西只需要_sqlite3.so
这一个文件。zlib
Python3.4
should be installed by yourself. The reason why2.7.5
can beimport zlib
, but3.4
>Cannot be downloaded, maybe because your3.4
library does not have thezlib
library, so you need to first confirm whether3.4
has itzlib./usr/local/bin/python3.4
in the terminal to enter the interpreter of3.4
, then enterimport zlib
to see if there is any reportImportError exception, if there is one, continue to read the content below, if everything is normal, leave me a message directly...🎜 🎜You need to try to recompile and install thezlib
corresponding to thePython
version to ensure that the correctzlib
is installed. I even recommend that you putzlib-devel
is also installed. 🎜 🎜But many times reinstalling does not work, or when installing using a package manager such asYum
, you will see something like "already exists" or "installed" during the installation process (of course the prompt is in English ), or it still cannot be used after forced installation. 🎜 🎜This is because it is still installed in the library directory ofPython2.7
. The solution is as follows: 🎜 🎜In the interpreter ofPython2.7.5
, enter the following code to view the 2.7 library directory: 🎜 rrreee 🎜Output result: 🎜 rrreee 🎜The most direct method is to add the required directories to the system path of Python3.4.First change your system's default Python to Python3.4 instead of Python2.7.5: 🎜 rrreee 🎜Enter
Python
in the terminal to enter the interpreter verification.Then run
vim ~/.bashrc
to edit the system environment variables. After entering editing mode, add this sentence at the end of the file:export PYTHONPATH=$PYTHONPATH:/usr/lib64/python2. 7/xxx/:/usr/lib64/python2.7/xxx/
, xxx is the library directory you need, separated by colon, then entersource ~/.bashrc
in the terminal to let it The current terminal is valid, or restart the device directly.You may ask me, how do I know which directory
zlib
is in? Uh, okay, help people to the end...Enter the
Python3.4
interpreter, andappend
the directories of the2.7
output above one by one. > Go to the list ofsys.path
, and then tryimport zlib
until no error is reported. Although the method is crude, it is effective. 🎜 🎜Although importing the entire directory is straightforward and convenient, it may conflict with the 3.4 library, so you'd better know which fileszlib
are, and thencopy
to3.4
's system path, for example,_sqlite3
only requires the file_sqlite3.so
.zlib
I don’t know, sorry...🎜 🎜Just give it a try, in theory it can be done,🎜--with-zlib
should mean you added it when compiling Pyton 3.4. This is obviously because your Python 2.7 version has zlib compiled but 3.4 does not.