Virtual Environment in Python for Linux and Windows

Virtual Environment in Python for Linux and Windows

Confused and lost while setting up a virtual environment?
No problem, I'm gonna guide you from the very beginning.

There are many ways to set up a virtual environment in python. We are going to set it up in a very simple way.

There are 5 steps for both Windows and Linux.

  1. Python Installation
  2. Installing virtualenv
  3. Setting Virtual Environment
  4. Activating and Deactivating virtualenv
  5. Selecting Interpreter

For Linux

  1. Python Installation
    We can install the desired version of python in various ways. I'll be discussing one of many ways.
    Go to the given site and download the desired version of python. Download Python
    Make sure you downloaded the .tar file. After downloading, open the terminal and change the directory to the Downloads and extract the downloaded file by running the following commands.
    tar -xvf filename.tar
    
    or
    tar xvzf filename.tar
    
    now go inside extracted directory and start the installation by running the following commands.
    ./configure
    make
    sudo make install
    

    After installing desired python version you need to update the path in the .bashrc file.

  2. Installing virtualenv
    After installation of the desired version of python, now is the time to install the python package virtualenv. For that run the following command.
    pip install virtualenv
    
  3. Setting Virtual Environment
    Open the terminal wherever you want to install the virtual environment.
    To set up a virtual environment you have to run the following command. Make sure you added the path to your new python environment.

    For example: "/usr/bin/python3.8"

    virtualenv --python="path to new python environment" venv3.8
    

    This venv3.8 is just a directory name of the virtual environment you created.

  4. Activating and Deactivating virtualenv
    For Activation
    Open a terminal in the venv3.8 directory or make sure you are in the venv3.8 directory and run the following command:

    source bin/activate
    

    For Deactivation
    Run the following command:

    source deactivate
    

    You can check the python version after activation python -V, you'll find the python version changed to the desired version.
    Also, you can check the path to python which python which will show you the path of virtualenv.

  5. Selecting Interpreter
    For VS Code

    • Open vscode and press Ctrl + Shift + p
    • Search option Python: Select Interpreter
    • Select Enter interpreter path...
    • Browse the created virtualenv folder (venv3.8) and inside bin folder select pythonversion(python3.8)

For Windows

  1. Python Installation and updating path.
    We can install the desired version of python in various ways. I'll be discussing one of many ways.
    Go to the given site and download the desired version of python. Download Python
    Make sure you downloaded the .exe file. After downloading, double-click the downloaded file and install it.

    Update Path in Environment variables after installation.

  2. Installing virtualenv
    After installation of the desired version of python, now is the time to install the python package virtualenv. For that run the following command in the command prompt.

    pip install virtualenv
    
  3. Setting Virtual Environment
    Open the command prompt wherever you want to install the virtual environment.
    To set up a virtual environment you have to run the following command. Make sure you add the path to your new python environment.

    For example: "C:\Users\Dell\AppData\Local\Programs\Python\Python37\python.exe"

    virtualenv -p "path-to-new-python-environment" venv3.8
    

    This venv3.8 is just a directory name of the virtual environment you created.

  4. Activating and Deactivating virtualenv
    For Activation
    Open a terminal in the venv3.8 directory or make sure you are in the venv3.8 directory and run the following command:

    .\Scripts\activate.bat
    

    For Deactivation
    Run the following command:

    deactivate
    

    You can check the python version after activation python -V, you'll find the python version changed to the desired version.
    Also, you can check the path to python Get-Command python which will show you the path of virtualenv.

  5. Selecting Interpreter
    For VS Code

    • Open VS Code and press Ctrl + Shift + p
    • Search option Python: Select Interpreter
    • Select Enter interpreter path...
    • Browse the created virtualenv folder (venv3.8) and inside Scripts folder select python version(python3.8).

Conclusion

I hope your confusion is over. Feel free to comment below if you have any queries.

Make sure you activate a virtual environment before kicking off. Most people forget this small thing time and again.

Cover Image Source ~ google.com/url?sa=i&url=https%3A%2F%2Fw..