Skip to content

Instantly share code, notes, and snippets.

@nullne
Last active February 2, 2016 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nullne/1defcad927f132b39b29 to your computer and use it in GitHub Desktop.
Save nullne/1defcad927f132b39b29 to your computer and use it in GitHub Desktop.

###Centos下编译安装python

####准备 主要是一些开发工具和其他的依赖包

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

####注意事项

Unicode

Python 在3.3版本以前的编码问题极为复杂,同时也令人苦恼。编译时加--enable-unicode=ucs4来开启Python2.7支持UTF-32,对于Python 3.2 加--with-wide-unicode

Shared Library

推荐以 shared library 的方式来编译Python.目前主流的Linus发行版都以这种方式提供Python环境,并且其他的一些第三方库,比方说 mod_wsgi, Blender, 也需要Python以 shared library 的形式。有两种方式:

  • LDFLAGS="-Wl,-rpath /usr/local/lib"加入编译命令中
  • 打开文件/etc/ld.so.conf,将路径/usr/local/lib加到文件结尾。
make altinstall

一般linux发行版都会自带某个版本的Python,所以在装自定义的版本的时候使用make altinstall而不是make install。这样系统中将同时保留这两种版本的Python,否则的话将会和系统自带的Python冲突。

####下载、编译、安装 # Python 2.7.6: wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz tar xf Python-2.7.6.tar.xz cd Python-2.7.6 ./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib" make && make altinstall

# Python 3.3.5:
wget http://python.org/ftp/python/3.3.5/Python-3.3.5.tar.xz
tar xf Python-3.3.5.tar.xz
cd Python-3.3.5
./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall

####下载安装Setuptools 和 pip # First get the setup script for Setuptools: wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py

# Then install it for Python 2.7 and/or Python 3.3:
python2.7 ez_setup.py
python3.3 ez_setup.py

# Now install pip using the newly installed setuptools:
easy_install-2.7 pip
easy_install-3.3 pip

# With pip installed you can now do things like this:
pip2.7 install [packagename]
pip2.7 install --upgrade [packagename]
pip2.7 uninstall [packagename]

python隔离环境

安装virtualenv, virtualenvwrapper

$ pip install virtualenv
$ cd my_project_folder
$ virtualenv venv
or
$ virtualenv -p /usr/bin/python2.7 venv

$ source venv/bin/activate

$ deactivate

FREEZE AND BATCH INSTALL
$ pip freeze > requirements.txt
$ pip install -r requirements.txt

VIRTUALENVWRAPPER

$ pip install virtualenvwrapper
$ export WORKON_HOME=~/.Envs               # make it invisible
$ source /usr/local/bin/virtualenvwrapper.sh
$ mkvirtualenv venv
$ workon venv
$ mkproject myproject
$ deactivate
$ rmvirtualenv venv

lsvirtualenv     List all of the environments.
cdvirtualenv     Navigate into the directory of the currently activated virtual environment, so you can browse its site-packages, for example.
cdsitepackages     Like the above, but directly into site-packages directory.
lssitepackages          Shows contents of site-packages directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment