1、python web课程资源
获取使用代码示例:https://github.com/dongweiming/web_develop
git pull --release origin master
本地部署一个docker环境,下载并使用封装好的镜像:
docker pull dongweiming/web_develop:dev
docker run -itd --name web_dev -p 9000:9000 -p 3141:3141 -p 5000:5000 dongweiming/web_develop:dev /bin/zsh
docker start web_dev
docker attach web_dev
sudo apt-get install python-pip -yq
sudo pip install pip -U -q
(venv)> which python
(venv) 00e9dedd02aa% which python
/home/ubuntu/web_develop/venv/bin/python
# coding=utf-8
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', 'flake8'])
'''
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()
import subprocess
import virtualenv
virtualenv_path = subprocess.check_output(['which', 'virtualenv']).strip()
EXTRA_TEXT = '''
ROOT_PATH = '/home/ubuntu/venv'
def extend_parser(parser):
parser.add_option(
'-r','--req', action='append', type='string', dest='reqs',
help="specify additional required packages", default=[])
def adjust_options(options, args):
if not args:
return
base_dir = args[0]
args[0] = join(ROOT_PATH, base_dir)
def after_install(options, home_dir):
if not options.reqs:
logger.warn('Warn: You maybe need specify some required packages!')
for req in options.reqs:
subprocess.call(['{}/bin/pip'.format(home_dir), 'install', req])
'''
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()
>virtualenv tmp
New python executable in /home/ubuntu/venv/tmp/bin/python2.7
Also creating executable in /home/ubuntu/venv/tmp/bin/python
Installing setuptools, pip, wheel...done.
Warn: You maybe need specify some required packages!
virtualenvwrapper.user_scripts creating /home/ubuntu/venv/venv1/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/ubuntu/venv/venv1/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/ubuntu/venv/venv1/bin/preactivate
virtualenvwrapper.user_scripts creating /home/ubuntu/venv/venv1/bin/postactivate
virtualenvwrapper.user_scripts creating /home/ubuntu/venv/venv1/bin/get_env_details
切换环境
- workon [env]
随时使用“workon 环境名”可以进行环境切换,如果不带环境名参数,则显示当前使用的环境
- deactivate
在某个环境中使用,切换到系统的python环境
/home/ubuntu/web_develop
more .env
source /home/ubuntu/web_develop/venv/bin/activate
cd web_develop
autoenv:
autoenv: WARNING:
autoenv: This is the first time you are about to source /home/ubuntu/web_develop/.env:
autoenv:
autoenv: --- (begin contents) ---------------------------------------
autoenv: source /home/ubuntu/web_develop/venv/bin/activate
autoenv:
autoenv: --- (end contents) -----------------------------------------
autoenv:
autoenv: Are you sure you want to allow this? (y/N) y
(venv) 00e9dedd02aa% which python
/home/ubuntu/web_develop/venv/bin/python
zsh的配置方法如下:
pip completion --zsh >> ~/.zprofile
source ~/.zprofile
bash的配置方法:
pip completion --bash >> ~/.bash_profile
普通权限用户使用pip安装工具包的方法:
pip install django --user
安装的包会放在当前用户的.local目录下。
注:在virtualenv环境中不能使用这种方法。
git clone https://github.com/dongweiming/vine
cd vine
pip install -e .
Obtaining file:///home/ubuntu/web_develop/vine
Installing collected packages: vine
Running setup.py develop for vine
Successfully installed vine
cd /
python -c 'import vine;print vine'
mkdir ~/.config/pip -p
cat ~/.config/pip/pip.conf
[global]
index-url = http://localhost:3141/root/pypi/+simple/
devpi-server --host=0.0.0.0 --stop
devpi-server --host=0.0.0.0 --start