为什么需要虚拟环境
运行不同环境的项目
安装虚拟环境(必要时 sudo)
pip3 install virtualenv
pip3 install vitrualenvwrapper
创建目录存放虚拟环境
mkdir $HOME/.virtualenvs
find / -name .virtualenvs
/root/.virtualenvs
添加全局
find / -name virtualenvwrapper.sh
/usr/local/bin/virtualenvwrapper.sh
使用命令:vim ~/.bashrc在里面添加下面内容
virtualenv definition
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
创建虚拟环境
Windows
安装:mkvirtualenv mywork
创建不需下载:mkvirtualenv mywork --no download
python@ubuntu:~$ which python3
/usr/bin/python3
python@ubuntu:~$ mkvirtualenv -p /usr/bin/python3 test
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/python/.virtualenvs/test/bin/python3
Also creating executable in /home/python/.virtualenvs/test/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
Done 安装成功后,自动切换 mywork 虚拟环境
注意:
1、虚拟机能联网
2、创建成功会自动工作在这个虚拟环境中
3、工作在虚拟环境中,提示符前面会有“虚拟环境名称”
进入虚拟环境
workon mywork
退出虚拟环境
deactivate
查看虚拟环境
workon
删除虚拟环境(退出才能删除)
rmvirtualenv mywork
查看虚拟环境目录
(mywork) python@ubuntu:~$ which python
/home/python/.virtualenvs/mywork/bin/python
默认python2
(mywork) python@ubuntu:~$ python
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
虚拟环境安装工具包
https://bootstrap.pypa.io/pip/这个网址上找到相应python版本,点开获取如下图:
pip install flask==1.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
(test) python@ubuntu:~$ pip -V
Traceback (most recent call last):
File "/home/python/.virtualenvs/test/bin/pip", line 7, in <module>
from pip._internal.cli.main import main
File "/home/python/.virtualenvs/test/lib/python3.5/site-packages/pip/_internal/cli/main.py", line 57
sys.stderr.write(f"ERROR: {exc}")
^
SyntaxError: invalid syntax
加上sudo 可以解决
sudo pip3 install flask==1.0
查看 sudo pip freeze