AllAboutOps

How to set up python environment on Windows - (PyEnv + Venv + VScode)

Author Avatar By Adarsh Mallandur on | 5 minute read | Category: Programming
How to set up python environment on Windows - (PyEnv + Venv + VScode)

If you have started to code using Python on your windows machine, chances are that you installed Python from the official website and then ran into some problem or you do not know where exactly to start because there are so many options available. Here is a guide to help find the best way to set up a python working environment on your windows machine. We will set up python on Windows by using Pyenv to install python, venv to create virtual environments, and vscode as IDE.

By the end of this post, you will set up python on windows by

  1. Understanding why we need multiple virtual environments with multiple python versions
  2. Using Pyenv along with venv to create and activate virtual python environments in Visual Studio Code

Why do we need virtual environments?

If you are going to install Python for the first time, you are almost certainly going to do it wrong! What one usually does is install Python from python.org website. You are having a system-wide installation of Python. You start using the python interpreter you installed and learn a few coding examples and write your first hello world code. And then you start a new project and install all sorts of 3rd party packages that are needed from the PyPi server like scikit-learn, tensorflow, etc.

Everything is going well until you decide to work on another new project and start installing new packages/different versions of existing packages. Then your previous projects start to break! You start fixing your previous project then your new project does not work!

Python Virtual Environments

This is why you should start installing Python the correct way and start using virtual environments from the beginning. Virtual environments help you to have any number of projects with their dependencies in an isolated environment without affecting the other projects. It is not system-wide and it does not interfere with other projects that you have in your machine.

Pyenv helps you install and manage multiple python versions without affecting your system-wide python installation. Please refer to my previous article to install python using Pyenv and then come back here.

Virtual Environment using PyEnv

Well, a virtual environment is just a directory with three important components:

  • site-packages/ folder where third party libraries are installed.
  • Symlinks to Python executables installed on your system.
  • Scripts that ensure executed Python code uses the Python interpreter and site packages installed inside the given virtual environment.

Python Virtual Environments

You can create virtual environments using various tools such as venv and virtualenv. Venv is part of Python’s standard library to create virtual environments. Virtualenv is a superset of venv and a powerful, extendable tool for creating isolated Python environments.

How to create virtual environment using PyEnv and venv?

Let us create a virtual environment inside the test project myproject , which allows us to install modules just for this project in a specific directory.

There are two ways of doing this

  1. Automatically using vscode
  2. Manually using command prompt

Automatic method

This guide assumes that you have visual studio code installed on your machine.

From within VS Code, you can create local environments, using virtual environments by opening the Command Palette (Ctrl+Shift+P), start typing the Python: Create Environment command to search, and then select the command. Choose Venv as the environment type

VSCode create environment

the command presents a list of interpreters that can be used as a base for the new virtual environment. Click on Enter interpreter path and Find and then navigate to the python interpreter of our pyenv version 3.10.7. You can find the path to pyenv version using the below command in your cmd

pyenv which python

After selecting the desired interpreter or Python version, a notification will show the progress of the environment creation and the environment folder will appear in your workspace.

VSCode created environment

Now you can see selected python virtual environment in the bottom right corner

VSCode python version

Manual method

Make sure you have set the required python version with pyenv global command.

pyenv global 3.10.7

Let’s create a folder for our project and go inside this folder.

mkdir myproject 
cd myproject

Let’s use the python virtual environment venv to create a virtual environment

python -m venv .venv

This will create a new directory called .venv which will include all the modules that we are going to install along with the python interpreter.

It is a good practice to create virtual environments in the same project folder by the name .venv and add the .venv directory to .gitignore file so that these modules are not tracked by git. We can then specify a requirements.txt file in order to maintain the versions of the modules.

To activate the virtual environment

.\venv\Scripts\activate

We can now see (.venv) appearing before the command prompt.

Now check the python version inside virtual environment

python 
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>>

To deactivate virtual environment

.\venv\Scripts\deactivate

Conclusion

In this post, we saw how to set up a python environment in a windows machine using PyEnv, venv and vscode. We can now start to develop python code in our windows machine for a given python version and maintain isolated virtual environments. Feel free to comment below if you need any help with the set up.