最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Python-Virtualenv

来源:动视网 责编:小采 时间:2020-11-27 14:26:44
文档

Python-Virtualenv

Python-Virtualenv:注释在使用virtualenv前ubuntu默认的解释器是python2.7,而且/usr/lib/python3里面已经安装好了ipython3和requests$ python Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux
推荐度:
导读Python-Virtualenv:注释在使用virtualenv前ubuntu默认的解释器是python2.7,而且/usr/lib/python3里面已经安装好了ipython3和requests$ python Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux


注释

在使用virtualenv前ubuntu默认的解释器是python2.7,而且/usr/lib/python3里面已经安装好了ipython3requests

$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ ipython3 
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: import requests

In [2]:

使用

由于一些兼容性问题,电脑上默认的python版本只能只能使用python2.7,所以创建命令要另外使用-p指定解释器

$ mkdir my_project_folder; cd my_project_folder

# 创建虚拟环境
$ virtualenv -p /usr/bin/python3 venv
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/ormsf/my_project_folder/venv/bin/python3
Also creating executable in /home/ormsf/my_project_folder/venv/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.

激活虚拟环境

$ source venv/bin/activate

现在可以看到提示符前面多了一个venv,代表虚拟环境创建成功

(venv) ~/my_project_folder $ ipython3

实践一下,虚拟环境和实际的环境隔离的

# 无法使用ipython3
(venv) ~/my_project_folder $ ipython3 
Traceback (most recent call last):
 File "/usr/bin/ipython3", line 4, in <module>
 from IPython import start_ipython
ImportError: No module named 'IPython'

# 默认的解释器已经变成了python3
(venv) ~/my_project_folder $ python
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

# 无法使用requests
>>> import requests
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ImportError: No module named 'requests'

注意不需要使用pip3

(venv) ~/my_project_folder $ pip install requests
Collecting requests
 Downloading requests-2.13.0-py2.py3-none-any.whl (584kB)
 100% |████████████████████████████████| 593kB 1.3MB/s 
Installing collected packages: requests
Successfully installed requests-2.13.0

现在request已经能够正确使用了

(venv) ~/my_project_folder $ python
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>
(venv) ~/my_project_folder $ pip install ipython

现在ipython也已经能够正确使用了

(venv) ~/my_project_folder $ ipython
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]:

退出

(venv) ~/my_project_folder $ deactivate

原理很简单,就是把系统Python复制一份到virtualenv的环境,用命令source venv/bin/activate进入一个virtualenv环境时,virtualenv会修改相关环境变量,让命令python和pip均指向当前的virtualenv环境。

更多Python-Virtualenv 相关文章请关注PHP中文网!

文档

Python-Virtualenv

Python-Virtualenv:注释在使用virtualenv前ubuntu默认的解释器是python2.7,而且/usr/lib/python3里面已经安装好了ipython3和requests$ python Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top