How to Create a venv with Global Packages in Python

If you are searching about creating a virtual environment (venv) with global packages in Python. We are here to fix it!

In Python, virtual environments allow you to create isolated environments for your projects, ensuring that dependencies and packages don’t clash. Then, you can use global packages within your virtual environment.

This guide will walk you through the steps to create a virtual environment (venv) with global packages in Python.

Why Use Virtual Environments?

Before we dive into the how-to, let’s quickly recap why virtual environments are essential:

  1. Isolation: It avoids conflicts between different projects.
  2. Reproducibility: You can recreate the same environment on different machines, ensuring consistency.
  3. Cleanliness: Your global Python installation remains clean and uncluttered.

Creating a venv with Global Packages in Python

Let’s create a virtual environment (venv) with global packages in Python:

Step 1: Install Python

First, ensure that Python is installed on the system. If not, download it from the official Python website.

Step 2: Install Virtualenv

While Python 3.3+ comes with the venv module, you might want to use virtualenv for more flexibility. Install it using pip:

pip install virtualenv
install venv with global packages in python

Step 3: Create a Virtual Environment

After that, move to the project directory. Finally, create a virtual environment. For including global packages, utilize the –system-site-packages option:

virtualenv --system-site-packages myenv
Create a venv with Global Packages in python

Else, if you’re using the venv built-in module, then execute the below command:

python -m venv --system-site-packages myenv

Step 4: Activate the Virtual Environment

Activate your virtual environment to start using it. The activation code line relies on your operating system:

  • Windows:
myenv\Scripts\activate
Activate the Virtual Environment
  • macOS/Linux:
source myenv/bin/activate

Once activated, the terminal changes to indicate that you’re working within the virtual environment.

Step 5: Verify Global Packages

Let’s list the installed packages to confirm that global packages are available in the virtual environment:

pip list
Verify Creating a venv with Global Packages

You see both the global packages and any packages installed within the virtual environment.

Step 6: Install Additional Packages

Let’s install matplot as an additional package in the project through pip:

pip install matplot
install additional packages

  1. Deactivate When Done
  2. When you’re done working in the virtual environment, deactivate it to return to your global Python environment:
deactivate
Deactivate environment
  1. Regularly Update Packages
  2. You can update all packages using:
   pip list --outdated
   pip install --upgrade package_name

Conclusion

Creating a virtual environment with global packages in Python is a straightforward process. It offers the best of both worlds: isolation for your project-specific dependencies and access to global packages.

To create a virtual environment with global packages in Python, use the –system-site-packages flag when creating the environment with virtualenv or venv. This allows you to access global packages while maintaining an isolated environment for your project.

By following the steps outlined in this guide, you can set up a virtual environment that meets your needs and keeps your development workflow smooth and efficient.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Scroll to Top