Learn how to create and manage virtual environments in Python for Windows, Mac, and Linux, to separate dependencies for different projects.
Learn how to create and manage virtual environments in Python for Windows, Mac, and Linux, to separate dependencies for different projects.
Python Virtual Environments: Learn to create, activate, and manage isolated dependencies for your Python projects
Python virtual environments are a way to separate the dependencies of different Python projects and keep them isolated from one another. In this tutorial, we will go through the steps to create and activate a virtual environment on different operating systems.
As a Python developer, it's essential to understand how to use virtual environments. Virtual environments allow you to separate the dependencies of different Python projects and keep them isolated from one another. This is especially important when you're working on multiple projects at the same time, or when you're collaborating with other developers. In this blog post, we'll go through the steps to create and activate a virtual environment on different operating systems.
Before we can create a virtual environment, we need to have Python installed on our system. If you don't already have Python installed, you can download it from the official website (https://www.python.org/downloads/). Once you have installed Python, you can verify that it is installed by running the following command in your command prompt or terminal:
#windows
python --version
Python 3.7.0
python -V
Python 3.7.0
#linux/mac
jai@MacBook-Air ~ % python3 --version
Python 3.7.0
jai@MacBook-Air ~ % python3 -V
Python 3.7.0
The next step is to install virtualenv, which is a tool that allows us to create and manage virtual environments. You can install virtualenv using pip, the Python package installer, by running the following command:
#windows
pip install virtualenv
#linux/mac
pip3 install virtualenv
Once virtualenv is installed, you can create a new virtual environment by running the following command:
virtualenv myenv
This will create a new directory called "myenv"in the current directory. This directory will contain all of the necessary files and dependencies for your virtual environment.
To activate the virtual environment, you need to run the activate script that is located in the "myenv" directory. The exact command to run will vary depending on your operating system:
jai@MacBook-Air mysitev1 % myenv\Scripts\activate
(myenv) jai@MacBook-Air mysitev1 %
jai@MacBook-Air mysitev1 % source myenv/bin/activate
(myenv) jai@MacBook-Air mysitev1 %
Once the virtual environment is activated, you should see the name of your virtual environment in the command prompt or terminal, indicating that it is active.
Now that you have an active virtual environment, you can install any packages or modules that you need for your project. To do this, simply run the following command:
pip install <package_name>
(myennv)pip install requests
(myenv) pip install django
When you are finished working on your project, you can deactivate the virtual environment by running the following command:
(venv) jai@MacBook-Air mysitev1 % deactivate
jai@MacBook-Air mysitev1 %
This will return you to the global Python environment, and any packages or modules that you installed in your virtual environment will no longer be available.
And that's it! You now know how to create and manage virtual environments in Python. Virtual environments are an essential tool for maintaining the integrity of your Python projects and keeping them isolated from one another. Whether you're working on Windows, Mac, or Linux, you can use this tutorial as a guide to setting up and managing your virtual environments.
In conclusion, Python virtual environments are a powerful tool for isolating the dependencies of different Python projects. By creating and activating a virtual environment, you can install and use the specific packages and modules that you need for your project, without affecting the global Python environment. This is especially important when you're working on multiple projects at the same time, or when you're collaborating with other developers. By following the steps outlined in this tutorial, you can create and manage virtual environments on Windows, Mac, and Linux. With virtual environments, you can ensure that your Python projects are stable and maintainable, and make sure that you can easily switch between different projects without any conflicts.
DigitalOcean Sign Up : If you don't have a DigitalOcean account yet, you can sign up using the link below and receive $200 credit for 60 days to get started: Start your free trial with a $200 credit for 60 days link below: Get $200 free credit on DigitalOcean ( Note: This is a referral link, meaning both you and I will get credit.)
👩💻🔍 Explore Python, Django, Django-Rest, PySpark, web 🌐 & big data 📊. Enjoy coding! 🚀📚