Here’s how to use virtual environments within Python projects to reduce the risk of version conflicts across Python projects.

Python projects often require the installation of packages or modules that extend the functionality of the standard Python language. They are typically installed using Python’s pip package manager. Extensions have versions. Conflicts can arise when multiple Python projects require different versions of the same package. So rather than installing Python packages system-wide, affecting all Python projects on a PC system, each project can have it’s own virtual environment in which packages can be installed. This means that the the Python package is installed local to that project, rather than for the whole PC system.

Python Virtual Environments

For detailed information, visit the official Python documentation, section Virtual Environments and Packages .

  1. Create a new directory, to contain your Python project, for example HelloWorld.
  2. Optional… it is good practice to use source control, like Git, to do that, open a terminal and enter
    1git init
    
  3. Within HelloWorld, open a terminal, create a new Python virtual environment, called “.venv”
    1python -m venv .venv
    
  4. If you created a git repository (with git init, mentioned above), you’ll want to exclude the .venv virtual environment from git
    1. in the project directory, create a file called .gitignore
    2. edit .gitignore and add .venv, save .gitignore
    3. if you want, you can add other directories or files to .gitignore
    4. for more information, see 2.2 Git Basics - Recording Changes to the Repository
  5. Start .venv
    1.venv\scripts\activate
    
  6. Install the Python packages your project requires
    1pip install SciPy
    2pip install Pandas
    
  7. Develop your Python project

Stopping & Restarting The Python Virtual Environment

To stop the .venv virtual environment, enter

1deactivate

To restart .venv, navigate to the directory containing the Python project and from the command line, use…

1.venv\scripts\activate

Distributing The Python Project To Users

To produce a file defining all the packages installed for the project, along with their version, use:

1pip freeze > requirements.txt

The requirements.txt file can then be committed to version control and shipped as part of the Python project. Users can install on their system all required packages & the specific versions you used, with:

1python -m pip install -r requirements.txt