使用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
,求大神帮忙!
怪我咯2017-04-17 17:37:13
CentOS-7
is installed by default with Python2.7.5
, as you enter after entering Python
under shell
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的库目录:
import sys
print '\n'.join(sys.path)
输出结果:
/usr/lib64/python27.zip
/usr/lib64/python2.7
/usr/lib64/python2.7/plat-linux2
/usr/lib64/python2.7/lib-tk
/usr/lib64/python2.7/lib-old
/usr/lib64/python2.7/lib-dynload
/usr/lib64/python2.7/site-packages
/usr/lib64/python2.7/site-packages/gtk-2.0
/usr/lib/python2.7/site-packages
最直接的方法是把所需的目录添加到Python3.4的系统路径中。
首先把你的系统默认Python改为Python3.4,代替Python2.7.5:
ln -fs /usr/local/bin/python3.4 /usr/bin/python
终端中输入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 why 2.7.5
can be import zlib
, but 3.4
>Cannot be downloaded, maybe because your 3.4
library does not have the zlib
library, so you need to first confirm whether 3.4
has it< code>zlib. Enter
/usr/local/bin/python3.4
in the terminal to enter the interpreter of 3.4
, then enter import zlib
to see if there is any report< code>ImportError 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 the zlib
corresponding to the Python
version to ensure that the correct zlib
is installed. I even recommend that you put zlib-devel
is also installed. 🎜
🎜But many times reinstalling does not work, or when installing using a package manager such as Yum
, 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 of Python2.7
. The solution is as follows: 🎜
🎜In the interpreter of Python2.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. Python
in the terminal to enter the interpreter verification. 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 enter source ~/.bashrc
in the terminal to let it The current terminal is valid, or restart the device directly. zlib
is in? Uh, okay, help people to the end...Python3.4
interpreter, and append
the directories of the 2.7
output above one by one. > Go to the list of sys.path
, and then try import 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 files zlib
are, and then copy
to 3.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,🎜大家讲道理2017-04-17 17:37:13
--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.