Python Activate Virtual Environment (venv)

How to activate venv virtual environment in Python 3? The venv module is used to create a lightweight virtual environment, this environment is created on top of the existing python installation hence it uses the same version as the current one.

In this article, I will explain how to activate the virtual environment on Windows, Linux, Unix, and Mac OS. Depending on your OS and the shell you are using the activating virtual environment takes different syntaxes.

OSShellCommand
Unix, Linux, or MacOSbash shellsource /path/to/venv/bin/activate
Unix, Linux, or MacOScsh shellsource /path/to/venv/bin/activate.csh
Unix, Linux, or MacOSfish shellsource /path/to/venv/bin/activate.fish
WindowsCommand Prompt\path\to\venv\Scripts\activate.bat
WindowsPowerShell\path\to\venv\Scripts\Activate.ps1
Activating Virtual Environment in Python

1. Create a Virtual Environment

First, let’s create a virtual environment. The below example creates a virtual environment dev-env under the current directory. Here, I will be using the virtual environment module venv that comes with Python 3.3 version.


# Create virtual environment
python3 -m venv dev-env

If you wanted to create at a custom location, just specify the absolute path along with the environment name.


# Create virtual environment
python3 -m venv /path/to/virtual/environment/dev-env

2. Activate Virtual Environment On Linux/MacOS in Python

Once an environment has been created, you may wish to activate it by sourcing an activate script in its bin directory.


# Activate virtual environment
source dev-env/bin/activate

Here, the Virtual environment dev-env has been activated to use. Any command you run after this will execute in a virtual environment.

3. Active Virtual Environment on Windows

On Windows, when you are using windows prompt you can activate Python virtual environment by running the activate.bat file from the bin directory, and when using PowerShell run the Activate.ps1 from the Scripts directory.


# Activate virtual environment on windows

# From command prompt
dev-env\bin\activate.bat

# From power shell
dev-env\Scripts\Activate.ps1

4. Check the Current Active Virtual Environment

Sometimes while working in the virtual environment, we may be required to get the environment you are in. There is no direct command to get this information, however, you can get it from the environment variable $VIRTUAL_ENV.


# Current virtual environment
# On Linux or MacOS
echo $VIRTUAL_ENV

# On windows
%VIRTUAL_ENV%

Frequently Asked Questions on Python Activate Virtual Environment

What is a virtual environment in Python?

A virtual environment is a self-contained directory that contains its own Python interpreter and libraries. It allows you to create isolated environments for different projects, each with its own dependencies, without affecting the system-wide Python installation.

Why should I use a virtual environment?

Using virtual environments helps manage project dependencies, preventing conflicts between different projects. It allows you to specify and control the version of libraries used in each project, ensuring consistency and reproducibility.

How do I create a virtual environment in Python?

To create a virtual environment in Python, you can use the built-in venv module. Here are the general steps.
Using Command Line or Terminal: Open a command prompt or terminal. Navigate to the directory where you want to create the virtual environment.
Activating the Virtual Environment: After creating the virtual environment, you need to activate it. The activation steps depend on your operating system.
Deactivating the Virtual Environment: To deactivate the virtual environment and return to the global Python environment.

How do I activate a virtual environment?

Activating a virtual environment in Python depends on the operating system you’re using. Once you’ve created a virtual environment.

Can I use the same virtual environment for multiple projects?

It is generally recommended to create a separate virtual environment for each project to avoid dependency conflicts. This ensures that each project has its own isolated environment.

What is the difference between venv and virtualenv?

venv is a built-in module in Python for creating virtual environments, while virtualenv is a third-party package that offers similar functionality. In recent Python versions, venv is recommended for most use cases, as it addresses some issues found in older versions of virtualenv.

Conclusion

In this article, you have learned how to activate a virtual environment in Windows, Linux, Unix, and mac OS. These methods are used only if you use the venv module. Note that depending on the OS and shell you are using the command to activate changes so keep an eye on what you are running.

Happy Learning!!

Naveen

Naveen (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn LinkedIn

Leave a Reply