Python环境配置中的报错及解决方案

python环境安装
环境:CentOS6.5
(1) centos下使用yum 安装pip失败
  
安装pip
yum install -y python-pip
(2) 安装过程中“ RuntimeError: Broken tooolchain:cannot link a simple c program"
安装gcc:yum install gcc 
安装g++:yum install gcc-c++
(3) 报错“ SystemError: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel.
yum install python-devel

(4) 若用到pyspark ,报错:
 “File "DT.py", line 6, in <module>
    from pyspark import SparkContext
ImportError: No module named pyspark
ImportError: No module named py4j.java_gateway“
实际上,spark包里面带有pyspark , importError是由于环境变量配置问题
环境变量设置
vi /etc/profile
export JAVA_HOME=/usr/local/jvm/jdk1.7.0_71
export HADOOP_HOME=/home/zkpk/hadoop-2.6.5
export SPARK_HOME=/home/zkpk/spark-2.0.1-bin-hadoop2.6
export HIVE_HOME=/home/zkpk/apache-hive-0.13.1-bin
export MAHOUT_HOME=/home/zkpk/apache-mahout-distribution-0.10.1
export PYTHONPATH=$SPARK_HOME/python:$SPARK_HOME/python/lib/py4j-0.10.3-src.zip:$PYTHONPATH
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$HADOOP_HOME/bin:$SPARK_HOME/bin:$HIVE_HOME/bin:$MAHOUT_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/tools/jar:$JAVA_HOME/dt.jar
#生效配置文件
source /etc/profile

(5)使用到bson, 报错“ I mportError: No module named bson
sudo pip uninstall bson
sudo pip uninstall pymongo
sudo pip install pymongo

(6)版本和包的问题

Cause :   Counter   is only supported in   python2.7   and higher and is not available in earlier versions -   Counter   class got added into   collections   package in   Python 2.7 .
在linux系统中默认的是python2.6版本,使用过程中报错。
(自测:Solution1和Solution2没有解决问题,Solution3解决)

Solution 1:As stated by Martin Pieters - use the backport.

Add counter.py at /lib64/python2.6/ - this is where the collections.py is ./lib64/python2.6/collections.py Patch collections.py with:

from counter importCounter

Solution 2:use the backport_collections package. Next patch (the import statement) the package you're getting exception at i.e. pandas in my case:

from backport_collections importCounter
Solution 3: 升级python2.6 到python 2.7

  
升级遇到的问题:
1. 如下命令更改软连接后,python 找不到
ln -s /usr/local/python2.7 /usr/bin/python
vim /etc/profile 增加python2.7的环境变量
2. 安装setuptools(区分版本)
报错: python内置的urllib模块不支持https协议的解决办法
安装  openssl-devel,并重新编译python
sudo pip numpy install 仍然报错
3. 更改软链接的方式或多或少会对系统有一定影响,建议采取下面方法
(1)下载Python2.7.13的包
tar -zvxf Python-2.7.13.tgz
cd Python-2.7.13
./configure --prefix=/usr/local/python2.7
make && make install

(2)安装 setuptools + pip
# First get the setup script for Setuptools:
wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py
# Then install it for Python 2.7 :(可能会遇到usrlib问题,见2.解决方案)
cd /usr/local/python2.7/bin
./python2.7 ez_setup.py
# Now install pip using the newly installed setuptools:
./easy_install-2.7 pip

# With pip installed you can now do things like this:
pip2.7 install [packagename]
pip2.7 install --upgrade [packagename]
pip2.7 uninstall [packagename]
(3)使用 virtualenv

# Install virtualenv for Python 2.7 and create a sandbox called my27project:
pip2.7 install virtualenv
virtualenv-2.7 my27project

# Check the system Python interpreter version:
python --version
# This will show Python 2.6.6

# Activate the my27project sandbox and check the version of the default Python interpreter in it:
source my27project/bin/activate
python --version
# This will show Python 2.7.X
deactivate
python虚拟环境的好处:http://liuzhijun.iteye.com/blog/1872241
(4) 在PYCHARM中添加环境File -> Settings -> Project Settings -> Python Intercepter -> Python Intercepters  Paths 选项卡的列表中添加自定义的导入路径即可。


版权声明:本文为zqqnancy原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。