How to quickly set up a Python virtual environment

Python Aug 31, 2021

Setting up a Python virtual environment is very beneficial and I recommend everyone to do it. In this post, I will show you how to quickly set up a virtual environment for Python and explain to you why it is so valuable.

1. Navigate to the right folder

Let's start with opening your terminal, press the windows key and type cmd and press enter. Next type cd and the name of your path where you want to store your Python virtual environment, in my example it is: cd C:\Users\tijnv\Documents\Codeonwards and press enter.

python_venv_1

2. Set up your Python virtual environment

To set up the virtual environment, use the following command: python -m venv project-name after that type the following command to check out your new virtual environment: start . you will see a new folder with your new virtual environment.

python_venv_2

3. Start your Python virtual environment

To start the Python virtual environment we need to type the following command: project-name\Scripts\activate.bat, make sure you are using backslashes \.

python_venv_2.5-1

Tip: To deactivate you can simply type deactivate within the Python virtual environment.

4. Create a new project inside

Open your project by clicking on the project, and in here you can either create a new project as I did or move one of your existing projects over.

python_venv_3

5. Why should I use a Python virtual environment?

The benefits of using a virtual environment are:

  1. Every virtual environment can have its own Python version and this is scoped within the virtual environment, this will avoid version collision.
  2. All the packages you install within the virtual environment are scoped within the virtual environment, this will avoid package collisions.
  3. If you accidentally screw something up within the virtual environment, you can simply set up a new one.

6. Conclusion

In conclusion, creating a Python virtual environment brings valuable benefits. By following the steps in this guide, you gain control over Python versions and avoid package collisions. Embrace virtual environments for a smoother coding experience.

Tags