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.
- Python Installation
- Installing virtualenv
- Setting Virtual Environment
- Activating and Deactivating virtualenv
- Selecting Interpreter
For Linux
- 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.
ortar -xvf filename.tar
now go inside extracted directory and start the installation by running the following commands.tar xvzf filename.tar
./configure make sudo make install
After installing desired python version you need to update the path in the .bashrc file.
- 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
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.
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 pythonwhich python
which will show you the path of virtualenv.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
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.
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
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.
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 pythonGet-Command python
which will show you the path of virtualenv.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..