Home  >  Q&A  >  body text

python3.x - centos下,python在virtualenv新建python3报错

使用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,求大神帮忙!

迷茫迷茫2741 days ago854

reply all(2)I'll reply

  • 怪我咯

    怪我咯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的目录逐个appendsys.path这个列表里,然后import zlib试过去,不报错为止,办法虽土,行之有效。

    导入整个目录虽然直接且方便,但很可能会和3.4的库有冲突,所以你最好能知道zlib是哪些文件,然后copy3.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.
    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 enter source ~/.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, 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. zlibI don’t know, sorry...🎜 🎜Just give it a try, in theory it can be done,🎜

    reply
    0
  • 大家讲道理

    大家讲道理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.

    reply
    0
  • Cancelreply