(坑集)Python环境配置

如题记录一些python环境配置中出现的坑:


一、ubuntu下使用pip安装mysqlclient包

    如果出现在ubuntu下安装mysqlclient包失败的情况下,可以先在使用apt-get安装libmysqlclient-dev,即:

sudo apt-get install libmysqlclient-dev

    然后再正常使用pip安装mysqlclient即可。


二、centos下使用pip安装mysqlclient包

    如果出现了下面这种问题,说明你正在使用python3,并且缺少静态文件

 #include "Python.h"
                        ^
    编译中断。
    error: command 'gcc' failed with exit status 1

    输入yum search devel|grep python,获取符合你python3版本的devel,安装后,mysqlclient安装成功。


三、virtualenvwrapper配置报错

    在安装好virtualenvwrapper之后,需要在~/.bashrc中配置,把vw的.sh文件添加进去,还可以选择修改虚拟环境默认保存的位置。

    首先在编辑.bashrc之前,你需要知道virtualenvwrapper.sh文件的路径,使用下面的命令查找

sudo find / -name virtualenvwrapper.sh

   下面是我的查找结果

    因为我是直接安装在我的用户路径下的,所以如果你是安装在根目录下,结果应该是/usr/local/bin/virtualenvwrapper.sh

    得知路径之后就可以在~/.bashrc文件中添加下面配置

export WORKON_HOME=$HOME/virtualenvs # 配置虚拟环境的保存位置 $HOME是用户的主目录
source /home/God/.local/bin/virtualenvwrapper.sh # 配置virtualenvwrapper命令的脚本 

    第一条配置可以随你的喜爱去更改,但是第二条配置需要使用刚才获取路径,只有命令脚本路径正确才可在linux中直接使用virtualenvwrapper中的命令。

    修改.bashrc文件后,需要重新加载.bashrc文件

source ~/.bashrc

重点!!!

    如果你的环境是python3,重新加载后,可能会出现这样类似的错误

/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks. 
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.

    因为在virtualenvwrapper.sh中有如下代码

# Locate the global Python where virtualenvwrapper is installed.
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ] then
    VIRTUALENVWRAPPER_PYTHON="$(command which python)"
fi

   脚本会默认使用python2环境,但是virtualenvwrapper装在了python3环境中,所以会有上面的报错。

   解决办法:

   直接将VIRTUALENVWRAPPER_PYTHON默认值修改为/usr/bin/python3即可

# Locate the global Python where virtualenvwrapper is installed.
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ] then
    VIRTUALENVWRAPPER_PYTHON="$(command which python)"
fi

    修改后,按照上面步骤,重新加载下virtualenvwrapper.sh即可解决问题。


原文地址:https://www.cnblogs.com/GF66/p/9785479.html