5 Steps To Setup Python Virtual Environments On Windows


Are you ready to take your Python development skills to the next level? If you're passionate about writing Python code and building powerful applications, then it's crucial to embrace the practice of using virtual environments. Trust me, it will revolutionize the way you develop and manage your Python projects!

In the world of Python development, virtual environments play a vital role in keeping your project dependencies isolated, maintaining consistency across different projects, and ensuring smooth collaboration with other developers. Whether you're a seasoned Pythonista or just getting started, virtual environments are an essential tool in your developer's arsenal.

In this step-by-step guide, I'll walk you through the process of setting up and using virtual environments on Windows machines using the venv module. I'll cover everything from installation to activation and demonstrate how virtual environments can help you avoid version conflicts, manage project dependencies effortlessly, and create a clean and organized development environment.

So, if you're ready to supercharge your Python development workflow, let's dive into the eleven easy steps to harnessing the power of virtual environments on Windows machines. Don't worry if you're new to this – I'll explain each step in detail and provide helpful tips along the way.

Let's get started!

Step 1: Creating the Directories

Before we dive into setting up virtual environments using venv, let's start by creating the necessary directories. While this step is a matter of personal preference, especially if you already have a specific directory structure in place, I'll guide you through creating two directories: "virtual_envs" and "pyvers.". My actual structure looks like this: <development folder>/<python_projects>/<virtual_envs>/<a bunch of virtual envs in their own folders>. Also inside the <python_projects> folder, I have the <pyvers>/<pyXXX> folders. Your milage will vary but if you;re fresh to Phython or programming in general, it' a good idea to have a development directory that you can keep all of your code inside.

Creating these directories will help you organize your virtual environments and Python versions in a neat and structured manner. Here's how you can create them:

  1. Open File Explorer on your Windows machine.

  2. Navigate to the location where you want to create the directories. This could be your user directory or any other location of your choice.

  3. Right-click in the desired location and select "New" from the context menu.

  4. Choose "Folder" from the submenu.

  5. Name the first folder "virtual_envs" (without quotes). This folder will serve as the parent directory for your virtual environments.

  6. Repeat steps 3 to 5 to create the second folder and name it "pyvers" (without quotes). This folder will house different Python versions.

Once you've completed these steps, you're ready to move on to the next step in setting up your virtual environment using venv.

Step 2: Downloading the Python Version

Before we can create a virtual environment, we need to have the desired Python version installed on our Windows machine. If you already have the Python version you want to use, you can skip this step. Otherwise, let's download and install the Python version of your choice by following these steps:

  1. Open a web browser and visit the official Python website at python.org.

  2. On the Python website, click on the "Downloads" tab located in the top navigation menu.

  3. You'll be presented with different Python versions to choose from. Select the version that suits your project requirements. It's generally recommended to choose the latest stable version unless you have specific reasons for using an older version.

  4. Once you've selected the version, scroll down the page to the downloads section. You'll find different installers for various operating systems. Look for the Windows installer appropriate for your system (32-bit or 64-bit) and click on it to start the download.

Step 3: Installing the Python Version

  1. Locate the downloaded file (e.g., python<ver number>.exe) and run it by double-clicking on it. This will launch the Python installer.

  2. IMPORTANT: Make sure to UNCHECK the "Add Python to PATH" option during the installation process. This is an important step as we will handle the environment configuration manually.

  3. Click on the "Customize Installation" button as we will be making additional customizations.

  4. In the customization window, click "Next" to proceed, unless you have specific options you want to modify. Feel free to uncheck the "Documentation" option if you don't need it, as it will reduce the installation size.

  5. Next, you will be prompted to choose the installation directory. Click on the "Browse" button and navigate to the "pyvers" directory you created earlier. Note that Python automatically suggests a directory name in the format "Python<version number>" for the version being installed. To maintain consistency, consider changing this directory name to "py<version number>". For example, you can set the installation directory to "...pyvers\py311" for Python version 3.11. If you plan to install different minor versions, you can add the specific minor version at the end of the directory name for clarity.

  6. Once you've selected the desired installation directory, click "Next" to proceed.

  7. The installer will now install Python in the chosen directory. After the installation is complete, a confirmation screen will be displayed.

Congratulations! You've successfully downloaded and installed the Python version of your choice, customized the installation directory, and ensured that Python is not added to the PATH automatically. With the Python version installed in the "pyvers" directory, we're now ready to move on to the next step, where we'll create our first virtual environment using venv on Windows.

Step 4: Creating a Virtual Environment

Now that we have the desired Python version installed and our directories set up, it's time to create our first virtual environment using the venv module. Follow these steps to create a virtual environment for your project:

  1. Open the Command Prompt on your Windows machine. You can do this by pressing the Win + R keys to open the Run dialog, typing "cmd" (without quotes), and pressing Enter.

  2. In the Command Prompt Type: <path to pyver>\<py ver>\python -m venv <path to virtual_envs>\<name of project>
    Replace <path to pyvers> with the actual path to your "pyvers" directory and <py ver> with the specific Python version you installed.
    Also, make sure to replace <path to virtual_envs> with the actual path to your "virtual_envs" directory, and <name of project> with the desired name for your project.

    For example, if you're creating a project called "my_awesome_project," the command would look like this: <path to pyver>\<py ver>\python -m venv <path to virtual_envs>\my_awesome_project

    This command will create a virtual environment named "my_awesome_project" within the "virtual_envs" directory.

  3. Press Enter to execute the command. The virtual environment will be created, and you'll see a new folder named "my_awesome_project" (or the name you chose) within the "virtual_envs" directory.

Great! You've successfully created a virtual environment for your project. This virtual environment will provide an isolated Python environment with its own installed packages and dependencies.

In the next step, we'll activate the virtual environment and start utilizing it for our Python development.

Step 5: Activating the Virtual Environment

Now that we have our virtual environment created, we can activate it and start using it for our Python development. Follow these steps to activate your virtual environment:

  1. Open the Command Prompt on your Windows machine. You can do this by pressing the Win + R keys to open the Run dialog, typing "cmd" (without quotes), and pressing Enter.

  2. In the Command Prompt, run the following command to activate your virtual environment:
    <path to your new venv>\Scripts\activate
    Replace <path to venv> with the actual path to your virtual environment. For example, if your virtual environment is named "my_awesome_project" and located in the "virtual_envs" directory, the command would look like this: <path to virtual_envs>\my_awesome_project\Scripts\activate

Running this command will activate the virtual environment, and you'll notice that the Command Prompt changes to reflect the activated environment.

Once the virtual environment is activated, you can start using Python and installing packages specific to this environment. Any Python packages you install or scripts you run will be isolated within this virtual environment, keeping your project dependencies organized and separate from the global Python installation.

Well done, you've successfully activated your virtual environment! Remember, whenever you're working on your project and want to access the virtual environment, you'll need to activate it using the activate command we just discussed. You can also use deactivate if you want to switch venvs, just used the same command with deactivate in place of activate.

Bonus: Setting Up VSCode and Pylance to Use the Virtual Environment (for VSCode Users)

If you're using Visual Studio Code (VSCode) as your preferred code editor (and found Pylance causing issues with "module not found" when it's clarly installed...), we've got you covered! Here's a bonus section on how to configure VSCode and Pylance to use the virtual environment we just created. Let's get started:

  1. In Visual Studio Code, Press Shift + Ctrl + P simultaneously to open the command palette. This can also be accessed by clicking on "View" in the top menu and selecting "Command Palette."

  2. In the command palette, type "python" and choose "Python: Select Interpreter" from the list of options that appears. Press Enter to select it.

  3. In the next box that appears, you'll see a list of available Python interpreters. Look for the option "+ Enter interpreter path..." and select it. This option allows us to specify the path to our virtual environment's Python interpreter.

  4. A textbox will appear, prompting you to enter the interpreter path. Enter the path to the Python interpreter within your virtual environment. The path should be in the following format: <path to virtual_envs>\<your_new_env>\Scripts\Python.exe

  5. After entering the interpreter path, press Enter to confirm your selection.

That’s all there is to it! You've successfully configured VSCode and Pylance to use the virtual environment's Python interpreter. Now, when you work on your project within VSCode, it will automatically use the Python interpreter from the virtual environment.

This setup ensures that any Python packages you install or dependencies you manage within the virtual environment will be utilized by VSCode, providing seamless integration between your code editor and the virtual environment.

Hopefully, this helps you to manage your Python projects in a cleaner and more manageable way!

Thank you for reading!