First step -install virtualenv:
sudo pip install virtualenv
Now create one python dev?environment
'>'virtualenv venv
New python executable in /home/ubuntu/venv/bin/python
Installing setuptools, pip, wheel...done.
Usersourceavtivate virtualenv?environment
'>'source venv/bin/activate
(venv)>wich python
/home/ubuntu/venv/bin/python
Quit
(venv)>deactivate
virtualenv定制化
生成virtualenv环境时同时安装flake8的自定义脚本:
'>'sudo chown ubuntu:ubuntu 'which virtualenv'
生成自定义脚本如下(create-venv-script.py):
import subprocess
import virtualenv
virtualenv_path = subprocess.check_output(['which', 'virtualenv']).strip()
EXTRA_TEXT = '''
def after_install(options, home_dir):
subprocess.call(['{}/bin/pip'.format(home_dir), 'install', 'flask8'])
'''
def main():
text = virtualenv.create_bootstrap_script(EXTRA_TEXT, python_version='2.7')
print 'Updating %s' % virtualenv_path
with open (virtualenv_path, 'w') as f:
f.write(text)
if __name__ == '__main__':
main()
生成脚本:
'>'python /''/create-venv-script.py
Updating /usr/local/bin/virtualenv
现在生成一个虚拟环境,就会自动安装flake8了:
'>'virtualenv tmp
New python executable in /home/ubuntu/web_dev/tmp/bin/python2.7
Also creating executable in /home/ubuntu/web_dev/tmp/bin/python
Installing setuptools, pip, wheel...done.
...
Installing collected packages: mccabe, pyflasks, pep8, flake8
Succerssfully installed flake8-2.5.4 mccabe-0.4.0 pep8-1.7.0 pyflakes-1.0.0
搭建python3开发环境
创建虚拟环境
virtualenv -p /usr/local/bin/python3.4 py34env激活
source py34env/bin/activate
退出虚拟环境
deactivate