Python-Virtualenv

高洛峰
Release: 2017-02-15 15:22:04
Original
1474 people have browsed it

Comments

Before using virtualenv, the default interpreter of ubuntu is python2.7, and it is already installed in /usr/lib/python3ipython3 and requests

$ 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.
>>>
Copy after login
$ 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]:
Copy after login

use

Due to some compatibility issues, the default python version on the computer can only use python2.7, so create The command must be additionally used -pSpecify the interpreter

$ 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.
Copy after login

Activate the virtual environment

$ source venv/bin/activate
Copy after login

Now you can see an extra ## in front of the prompt #venv, represents the successful creation of the virtual environment

(venv) ~/my_project_folder $ ipython3
Copy after login
Practice it, the virtual environment is isolated from the actual environment

# 无法使用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'
Copy after login
Note that there is no need to use 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
Copy after login
Request now It has been used correctly

(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
>>>
Copy after login
(venv) ~/my_project_folder $ pip install ipython
Copy after login
Now ipython can also be used correctly

(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]:
Copy after login
Exit

(venv) ~/my_project_folder $ deactivate
Copy after login
The principle is very simple, that is, copy the system Python to the virtualenv environment , when using the command

source venv/bin/activate to enter a virtualenv environment, virtualenv will modify the relevant environment variables so that the commands python and pip point to the current virtualenv environment.

For more Python-Virtualenv related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template