How To Create Virtual Environments For Your Python Projects
Have you ever heard of virtual environments? If you are a beginner, then you might not have heard of them, but if you are an experienced Python developer, then they must be a part and parcel of your life. Creating a separate virtual environment for each of your Python projects is a good practice for it prevents various dependency and compatibility issues and keeps your main system clean.
The main purpose of Python virtual environments is to create an isolated environment for each of your projects. For example, if you want to use an older version of a library or module in one project and the recent version in another, then you can conveniently create separate virtual environments for each of these projects.
In this blog post, I'll cover how you can easily create and use a virtual environment using virtualenv in 9 easy to follow steps.

1. Install virtualenv
In order to install it just type the following command in the shell.
pip install virtualenv
2. Test your installation
Type this command in the shell, and if your installation is successful, the installed version will be displayed on the command line terminal.
virtualenv --version
3. Make the project directory
Next step is to create a directory for your project, which can be achieved by this command.
mkdir my-project-dir
You can use any name for the directory, I have chosen 'my-project-dir'.
4. Switch to the project directory
With this command, you will switch from your current working directory to your project directory.
cd my-project-dir
5. Create the virtual environment
To create your virtual environment in the project directory, you can use the following command.
virtualenv venv -p C:\Python\Python36\python.exe
'venv' is the name for your virtual environment (you can choose any name) and 'C:\Python\Python36\python.exe' is the path to the python.exe file of the version of Python that you want to use for the project. I have used python 3.6 here. This path might vary from system to system and you need to locate the path to python.exe file.
6. Activate the virtual environment
If you want to activate the virtual environment that you just created, simply type this command.
. .venv\Scripts\activate
If this command works successfully, you would see a '(venv)' in the beginning of your shell command line.
7. Verify the python version
You might want to verify the python version inside of your virtual environment just to be sure that you have created the environment with the desired python version.
python --version
8. Check the installed packages
To see what all python packages are installed in your environment, type the following command.
pip list
9. Deactivate the virtual environment
To get out of your virtual environment, simply type this command in the shell.
deactivate
If you've any queries, feel free to post them in the comments section. If you found this post helpful, then share it with a friend! Happy Coding :)
Comments
Post a Comment
Feedbacks and suggestions are welcomed...