How to install python3-venv on Ubuntu 16.04
Since Ubuntu 16.04 is a very popular operating system, it is one operating system that you will likely come across when you are building software with Python 3. For example, if you want to get a Nvidia Jetson TX2 developer kit today, you will find yourself dabbling with Ubuntu 16.04.
On the other hand, the venv module of Python 3 is one recommended way to create virtual environments for our Python 3 projects.
In case you need it, this post describes how to install python3-venv
on Ubuntu 16.04.
Add universe repository and update Ubuntu 16.04
Open up a terminal window. After your terminal window had started, add Universe repository and upgrade your Ubuntu with the following commands:
sudo apt-get install software-properties-common -y sudo apt-add-repository universe sudo apt-get update
After the commands complete, you should be able to install python3-venv
.
Installing python3-venv on Ubuntu 16.04
Next, run the following command to install python3-venv on Ubuntu 16.04:
sudo apt-get install python3-venv -y
Creating a virtual environment for your Python 3 application with python3-venv
Once python3-venv
had been installed successfully, you should be able to create a virtual environment for your Python 3 application.
In order to test that out, run the following command to create a virtual environment for your Python 3 application inside ~/my_py3_venv
:
python3 -m venv ~/my_py3_venv
After the command complete, you should be able to find the virtual environment inside the ~/my_py3_venv
directory.
Getting into your Python 3 virtual environment with your terminal window
In order to get into your Python 3 virtual environment with your terminal window, you need to run the activate
script. Given that, run the following command to get into your Python 3 virtual environment:
source ~/my_py3_venv/bin/activate
After the command complete, you will find that your terminal prompt is appended with (my_py3_venv). While inside this virtual environment, you can install any Python dependencies with the pip
command. For example, if you wish to install Flask, you can run the following command:
pip install flask
When you had installed the necessary dependencies for your Python 3 application, you can then run your application with the python
binary:
python run_your_app.py
Getting out of your Python 3 virtual environment
In order to get out of your Python 3 virtual environment, you need to run the following command:
deactivate
After the command completes, you will find that your terminal prompt is not appended with (my_py3_venv). At this point in time, whatever dependencies that you had added while inside the virtual environment will not be available.