Creating Virtual Environments for Python Projects in VS Code
Want to share your content on python-bloggers? click here.
![Python Virtual Environments: A Primer – Real Python](https://i0.wp.com/files.realpython.com/media/Python-Virtual-Environments_Watermarked.4c787192d42f.jpg?resize=578%2C325&ssl=1)
I had a similar problem recently, and then a request came through from a close friend (Chris Mainey) for the same purpose. I thought “I’ll write a blog post on this”. So, what are the benefits of creating virtual environments.”
First of all. what are virtual environments:
A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system
Python Website Definition
Okay, so what are the benefits?
- You can use any version of python you want for a specific environment without having to worry about collisions
- You can organize your packages much better, and know exactly the packages you need to run your code, in case someone else needs to run it on their machine
- Your main python package directory does not get FLOODED with unnecessary python packages
The only downside might be space on your machine, as potentially you could be doubling up with installs.
If this sounds like something you would like to do in your projects, then let’s get going, as the following sections and sub-sections will take you through:
- Installing a virtual environment via Windows Command Prompt, would work the same way in Windows Powershell
- Getting VS code to recognise the new virtual environment
- Installing packages into the new virtual environment
Getting familiar with Windows Command Prompt
- Navigate to Windows Command Prompt:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-3.png?resize=508%2C345&ssl=1)
This is the first stage. Now we need to navigate to the folder we are going to be working with for our Python project.
Changing directory to our Python project folder
- To change directory to the project folder, in command prompt, we will use the cd command. This changes directory (cd) and will allow you to specify which directory you need to change it to:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-4-edited.png?resize=543%2C634&ssl=1)
2. I got here by using change directory to my documents folder, analysed the contents of the directory using dir and then used change directory again to set the path equal to the PythonVirtual Environment folder (this was created in my Documents folder). This could have been done in one step by using: cd C:\Users\[yourusername]\Documents\PythonVirtualEnvironment:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-5.png?resize=578%2C150&ssl=1)
Now I am in the right place to be to run the virtual environment creation commands.
Installing the new Python virtual environment
- The next step is to use the following command python -m venv env to setup the virtual environment:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-6.png?resize=578%2C188&ssl=1)
2. To deconstruct what this is doing:
- Python tells command prompt to use the local installation of Python
- The venv is the command to python to make a virtual environment – this is static and does not change
- The env is the name of the subdirectory that will be created inside to hold the virtual environment
3. Once I hit return we will see that the virtual environment has now been created. I will inspect this by using the dir command:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-7.png?resize=552%2C503&ssl=1)
4. Inspecting this in the folder structure, we can see that the Python structure has now been created for us:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-8.png?resize=578%2C134&ssl=1)
Violla! We have our new virtual environment ready to go. The next step is to make VS code recognise it.
Getting VS code to recognise the new virtual environment
- Go to the root directory, right click and choose Open with VS Code:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-9.png?resize=578%2C531&ssl=1)
2. Click to open with VS Code. This will launch VS Code. When it has loaded we need to create a new directory for the name of the project:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-10.png?resize=578%2C325&ssl=1)
3. I will click on the new folder icon and name the project whatever I want:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-11.png?resize=578%2C411&ssl=1)
Once a Python files is detected, the new virtual environment is normally detected, assuming that it doesn’t, I will show you the manual setup process.
Setting the VS Code interpreter to work with the new virtual environment
To do this you will need to follow the relevant steps below:
- Click on the interpreter selected at the bottom left of VS Code:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-12.png?resize=578%2C246&ssl=1)
2. Click on the current Python version
3. This will launch the Select the Interpreter window:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-13.png?resize=578%2C268&ssl=1)
4. Click to Enter the interpreter path:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-14.png?resize=578%2C72&ssl=1)
- Click Find…
- This will launch a dialog box. We are going to navigate to the env folder we have just created and select the Python.exe:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-15.png?resize=578%2C363&ssl=1)
7. Choose select interpreter button. You should now see the new environment that we have created being activated:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-16.png?resize=572%2C270&ssl=1)
8. Our new virtual environment has now been picked up by VS Code and is ready to use.
Installing packages into the new virtual environment
To install a specific version, type the package name followed by the required version: >>pip install ‘PackageName==1.4’. This tutorial will focus on the most recent package install process, but this can be used to set specific references. The process is as follows:
- The virtual environment will reference roaming installs of the Python packages, but to install any local ones I will navigate to my Scripts folder in the PowerShell terminal in VS code by using:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-17.png?resize=578%2C89&ssl=1)
Note: here I used ../ in PowerShell / Command Prompt – this is a way of saying go back one directory step.
2. I will use pip install to install the required package:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-18.png?resize=578%2C25&ssl=1)
3. I have chosen to pip install Flask-SocketIO from the Python packages list. Hitting return you will see the package installing:
![](https://i0.wp.com/hutsons-hacks.info/wp-content/uploads/2021/04/image-19.png?resize=578%2C250&ssl=1)
4. This has now installed a new package into my virtual environment.
The process is now complete and you can create code files in your new project folder to your hearts content.
Final thoughts
I think virtual environments are way cool. They can streamline your code and are great for development, deployment and testing. By isolating the modules into separate folders, you have complete control of the package infrastructure for your projects.
Want to share your content on python-bloggers? click here.