Installing Python in Ubuntu14.04 is relatively easy. The easiest way to install Python is apt-get. For details, see this article: how to install Python in Ubuntu14.04 and switch between py2 and py3 environments. Today, I will share with you how to create Python virtual environment in Ubuntu14.04. Virtualenv is also required to create virtual environments in Ubuntu, as in Windows, so install virtualEnv first. Enter sudo PIP install virtualenv on the cli and wait until the installation is complete, as shown in the following figure.
If the installation fails, run the sudo apt-get install python-virtualenv command, as shown in the following figure.
2. Then type PIP list on the command line to view the installed package and see that virtualEnv is already in it.
3. In the current directory or switch to a folder where you want to create a virtual environment, enter the command “Virtualenv demo”. Demo is the name of the virtual environment, as shown in the picture below.
4. Go to the virtual environment demo and go to the bin directory. The files in the bin directory are different from the virtual environment in Windows, and the methods for activating the virtual environment are different, as shown in the following figure.
5. Enter the “Source activate” command to activate the virtual environment. Demo is surrounded by parentheses, as shown in the following figure.
If you type Python, you can see the Python3 environment. 6. If you want to create a Python2 virtual environment, go back to the folder where you want to create a Python2 virtual environment and type “virtualenv -p /usr/bin/python2 Py2”, where Py2 is the name of the virtual environment, as shown in the following figure.
7. As with Python3 virtual environments, go to Py2, go to the bin directory, and type “Source Activate” to activate the virtual environment, as shown below, with Py2 enclosed in parentheses.
Note 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. If you type Python into the PY2 virtual environment, the system will automatically switch to Python2, as shown in the following figure.
9. To exit the virtual environment, enter the command “deactivate”, as shown in the following figure.
Next, you can create Python virtual environments and switch Python environments in Ubuntu.