When you start working on a new project in Python, there are some good practices to follow, especially regarding the working environment and dependencies.

A virtual environment is a separate Python environment where the Python interpreter, libraries, and scripts installed inside are isolated from other environments. It like a an additional disposable Python installation on your computer, that you can turn on and off at will, and which contains its specific packages and dependencies.

Quick start commands

Here is a small library of commends to use in your projects to perform specific task quickly.

Create and Delete a Virtual environment using venv

# Create 
python3 -m venv env
source env/bin/activate
# Delete
rm -r /path_to_project/env
deactivate

Create and Delete a temporary Virtual environment using pipenv

# Create 
pipenv shell
# Delete
exit

Install all dependencies after cloning a project

# If project root folder contains a `requirements.txt` file
pip install -r requirements.txt 
# If the project root folder contains a `Pipfile.lock` file
pipenv install

Generate or Update requirements.txt

pip install package_name_1 package_name_2
pip freeze > requirements.txt

Generate or Update Pipfile.lock

# If `requirements.txt` doesn't exist yet:
pip install package_name_1 package_name_2
pip freeze > requirements.txt
# Generate `Pipfile.lock`:
pipenv install

Virtual environment: commands explanation

Here is a list of the most usefull commands to use a virtual environment with python, with some explanations.

Preriquisites

To run a virtual environment in Python, there are 3 libraries available: 

  • venv: A package shipped directly with python3 (no need to use the python installer pip)
  • virtualenv: An independent library (link) that can be install with: pip install virtualenv.
  • pipenv: This package creates a temporary environment using virtualenv (no folder created inside the project) and generate a Pipfile.lock

The first two solve the same problem and work in a very similar manner: they store the dependencies in a portable way (inside the project folder). If you use python3, I recommend to use venv to avoid an extra dependency. The last one allows to run a virtual environment quickly (with just one simple command: pipenv shell) but stores the dependencies in a non-portable way (somewhere on the computer, outside of the project folder).

Create & Remove

  • Create a virtual Environment: 
    • virtualenv env (where env is the name of the forlder that will be created) 
    • pipenv shell if you use pipenv
  • Create a virtual environment with a specific interpreter: virtualenv -p /usr/bin/<interpreter> env (replace <interpreter> by python3, python2.7,...)
  • Remove the virtual environment: rm -r /path_to_project/env (eg. rm -r /var/www/portfolio/env)

Activate & Deactivate

  • Activate the virtual environment: source env/bin/activate
  • Desactivate the current virtual env.: deactivate
  • If using pipenv: exit

Dependencies: commands explanations

Using requirements.txt

The folowwing commands should be ran when being already inside a virtual environment (previous section) even though they work outside of a virtual environment as well.

  • pip freeze > requirements.txt: 
    • Generates a requirements.txt file
    • This is usefull if you create a project from scratch
  • pip install -r requirements.txt:
    • Installs all the dependencies listed in requirements.txt
    • This is usefull if you clone a project,

Using Pipfile.lock

A setup using requirements.txt can imply security vulnerabilities. That is why it is recommend to use a Pipfile.lock file instead.

  • pipenv shell: 
    • Creates a temporary virtual environment (no additional folder is created inside the project).
    • If the project contains a requirements.txt, this command will generate a Pipfile.lock based on it. Otherwise it will create an empty Pipfile.locker.
    • Get out of this virtual env. using exit
  • pipenv install:
    • If the project contains a requirements.txt, this command will generate a Pipfile.lock based on it. Otherwise it will create an empty Pipfile.locker.
    • Then it installs all dependencies from Pipfile.lock

Other usefull Python commands

  • pip list: List Python packages in the current environment (local or virtual)
  • pip install package_name: install a package (eg. pip install numpy)
  • pip uninstall package_name: remove a package  (eg. pip uninstall numpy)

To go further:

⇐ Python: Deploy a Cron job on...Python Script automation on... ⇒
Image
Author of this website. I am passionate about programming and web design since the age of 12. After a stint in architecture and entrepreneurship, I came back to my original passion at 30 years old and became a self-taught web developer in less than 2 years.

Leave a Comment