Creating Virtual Environment in Python with Possible Errors

Nishitha Kalathil
5 min readDec 14, 2023

--

We have already discussed about virtual Environments in computer programming. You can check that story here. A virtual environment in Python is a self-contained directory that houses a Python interpreter and its standard library. It allows you to create an isolated environment for a Python project, with its own dependencies and packages, separate from the system-wide Python installation. This is useful when you’re working on multiple projects with different requirements or when you want to avoid conflicts between dependencies.

This time we can see how to create a virtual environment in Python. Python is the most simple and beautiful language that I have ever seen. Its only few steps to create a virtual environment in Python. Lets do it step by step.

There are multiple ways to create a virtual environment in Python, and the choice depends on your preferences and the Python version you are using.

1. Using venv (built-in in Python 3.3 and later)

  1. Open a terminal or command prompt.
  2. Navigate to the directory where you want to create your virtual environment.
  3. Run the following command:

python -m venv myenv

Here ‘myenv’ is the name of your virtual environment. You can use any name. After running the above code you can see a new folder , ‘myenv’ is created in your project folder.

Now you have to activate this virtual environment. The activation file will be inside the ‘Scripts’ folder which is inside our ‘myenv’ folder.

So you have to give the following command;

myenv\Scripts\activate

The above code is for Windows. If you are using Mac os, use the following command;

source myenv/bin/activate

Now your virtual environment is activated and you can see your project path is inside your created virtual environment.

It is important to note that every time you are opening your project, don’t forget to activate your virtual environment.

Once you’ve activated the virtual environment, you can install packages using pip, and they will be isolated to that environment. When you're done working in the virtual environment, you can deactivate it using the deactivate command.

Possible Errors

Most common error that i have seen while creating the virtual environment is UnauthorizedAccess.

This is an error happened because of PowerShell’s script execution policy in your system. By default, PowerShell may prevent the execution of scripts for security reasons. To run scripts, you need to change the execution policy.

In order to solve this error, you have to go to your start menu/Windows button and type ‘PowerShell’ and choose ‘Run as Administrator’.

In the PowerShell Window you have to check the Execution Policy by giving the command, ‘Get-ExecutionPolicy’ and you can see it as ‘Restricted’.

In order to run our script, we need to change the ExecutionPolicy to ‘RemoteSigned’. For that you have to run the following command ‘Set-ExecutionPolicy RemoteSigned’ in the window.

You have to confirm the change by typing ‘Y’ for yes.

Now you can go back to your project terminal and activate your virtual environment.

2. Using virtualenv (third-party package)

‘virtualenv’ is a third-party Python package that provides tools for creating isolated Python environments. First we have to install this package by running the below code in your terminal.

pip install virtualenv

Now you can create the virtual environment using the given code in the terminal.

virtualenv myenv

Then you have to activate the virtual environment using the below code.

myenv\Scripts\activate

3. Using pipenv

‘pipenv’ is another tool for managing Python project dependencies and virtual environments. It simplifies the process of creating and managing virtual environments, while also handling the installation of project dependencies.

You can creating virtual environent using pipenv by following steps.

  1. Install pipenv using the command ‘pip install pipenv’
  2. Create virtual environment using ‘pipenv install’
  3. Activate the virtual environment using ‘pipenv shell’

4. Using conda (if using Anaconda distribution)

‘conda’ is a package management and environment management system for multiple programming languages, including Python. With conda, you can create, export, list, remove, and update environments directly using simple commands.

To create a virtual environment using conda , use the following command in the terminal or command prompt of your operating system.

conda create — name myenv

You can also specify a Python version when creating the environment.

conda create — name myenv python=3.8

After creating the virtual environment, you need to activate it. For Windows you can give the command;

conda activate myenv

On macOS/Linux

source activate myenv

Once you activate your virtual environment, you can add packages like given below;

conda install numpy pandas

When you’re done working in the virtual environment, you can deactivate it by;

conda deactivate

The choice of a virtual environment creation method depends on your specific needs and preferences. For general Python projects, built-in venv or virtualenv is often sufficient. If you're working with data science or need better management of non-Python dependencies, conda is a strong choice. For modern Python development workflows with a focus on both virtual environments and dependencies, pipenv is a popular option. Consider the specific requirements of your project and choose the tool that best fits your workflow.

Hope you enjoy this article. If you are a beginner in Python, you can check my previous articles here….

Happy coding!!!!

--

--