It is relatively easy to install Python in Ubuntu 14.04. The easiest installation method is to install it with apt-get. For specific tutorials, you can click this article: How to install Python3 and switch between py2 and py3 environments in Ubuntu14.04. Today I will share with you how to create a Python virtual environment on Ubuntu 14.04. The specific tutorial is as follows.
1、 Like Windows, creating a virtual environment in Ubuntu also requires virtualenv, so you must install virtualenv before creating a virtual environment. Enter the command "sudo pip install virtualenv" in the command line and wait for the installation to complete, as shown in the figure below.
If the installation of the above method is not successful, you can try to enter the command "sudo apt-get install python-virtualenv" to install, as shown in the figure below.
2、 After entering pip list in the command line, you can view the installed packages, and you can see that virtualenv is already in it.
3、 Then enter the command "virtualenv demo" in the current directory or switch to a folder where you want to create a virtual environment, where demo is the name of the virtual environment, as shown in the following figure, the virtual environment is created successfully.
4、 Enter the virtual environment demo, and then enter the bin directory. The files inside are not consistent with the virtual environment under Windows, and the method of activating the virtual environment is also inconsistent, as shown in the following figure.
5、 Enter the command "source activate" to activate the virtual environment. At this time, the demo has been enclosed in brackets, as shown in the figure below.
Enter Python at this time, you can see that it is the Python3 environment.
6、 If you want to create a virtual environment of Python2, you need to go back to the folder where you want to create the virtual environment and enter the command "virtualenv –p /usr/bin/python2 py2", where py2 is the name of the virtual environment, as shown in the figure below Show.
7、 The same as the virtual environment of Python3, enter py2, then enter the bin directory, enter the command "source activate" to activate the virtual environment, as shown in the figure below, at this time py2 has been enclosed in brackets.
It should be noted that before switching to the py2 virtual environment, you need to enter the command "deactivate" to exit the previous demo virtual environment, as shown in the figure above.
8、 At this time, enter python in the py2 virtual environment, you can see that the system will automatically switch to the Python2 environment, as shown in the figure below.
9、 If you want to exit the virtual environment, you only need to enter the command "deactivate", as shown in the figure below.
Next, friends can freely create Python virtual environments and switch Python environments in Ubuntu.
Recommended Posts