Working with Python, you might have come across the need for segregating project environments. A useful tool for this purpose is the Virtualenv. In this tutorial, we shall guide you on how to make a Python 3 Virtualenv.
What is Virtualenv?
Virtualenv is a Python library used to create isolated Python environments. It keeps the dependencies required by different projects in separate places by creating isolated Python virtual environments for them. So, every project can have its own dependencies, irrespective of what dependencies every other project has.
Step 1: Install Virtualenv
If Virtualenv is not installed, install it using pip:
1 |
pip install virtualenv |
Step 2: Test your Installation
Once installed, you can check your installation with the following:
1 |
virtualenv --version |
Step 3: Create a Python 3 Virtual Environment
Now let’s create a new virtual environment using Python 3:
1 |
virtualenv -p python3 myenv |
Step 4: Activate the Virtual Environment
After creating a Virtual Environment, you can activate it using:
Windows:
1 |
.\myenv\Scripts\activate |
Linux/Mac:
1 |
source myenv/bin/activate |
Complete Code
Here is the complete implementation for creating and activating a Python 3 Virtualenv.
1 2 3 4 5 |
pip install virtualenv virtualenv --version virtualenv -p python3 myenv .\myenv\Scripts\activate (for Windows) source myenv/bin/activate (for Linux/Mac) |
Conclusion
The creation of a virtual environment can streamline your work and keep the dependencies of different projects separate, ensuring no conflicts occur. You can read more on the subject to further refine your use of Python 3 virtual environments.