Virtual environments in Python

A virtual environment, or “venv,” is a tool that helps to keep dependencies for different projects separate by creating isolated Python environments. Each virtual environment has its own installation directories, which means that packages installed in one environment will not affect packages in another.

There are several tools available for creating virtual environments in Python, but the most commonly used are virtualenv and venv. Here’s an example of how to create a virtual environment using venv on Windows, Mac, and Linux:

Windows

  • Open the command prompt and navigate to the directory where you want to create the virtual environment.
  • Run the following command: python -m venv myenv (replace “myenv” with the name of your environment)
  • Activate the virtual environment by running myenv\Scripts\activate.bat

Mac/Linux

  • Open a terminal and navigate to the directory where you want to create the virtual environment.
  • Run the following command: python3 -m venv myenv (replace “myenv” with the name of your environment)
  • Activate the virtual environment by running source myenv/bin/activate

Once you’ve activated a virtual environment, you can install packages using pip, which is a package manager for Python. You’ll notice that the name of the environment is now displayed in the command prompt or terminal, indicating that the virtual environment is currently active.

If you want to deactivate the virtual environment, you can simply run deactivate command. And to delete the environment, you can delete the environment folder, with the name you have provided while creating it.

By using virtual environments, you can ensure that the dependencies of your projects do not interfere with each other, which can be particularly useful when working on multiple projects that have different dependencies.

You can use pip to install packages inside virtual environment. Here are the basic steps for installing a package using pip:

  1. Open a command prompt or terminal, and activate your virtual environment if you haven’t done so already.
  2. Run the command pip install package_name, replacing “package_name” with the name of the package you want to install.
  3. You can also use pip install package_name==version_number to install specific version of package
  4. Pip will download and install the package, along with any dependencies.
  5. To confirm that the package was installed correctly, you can run pip list to see a list of all the packages currently installed in your virtual environment.

You can also use pip to uninstall packages by running the command pip uninstall package_name.

It’s also a good practice to create a file called “requirements.txt” in your project directory, in which you list all the packages that your project depends on. This makes it easy for others to recreate the same environment on their own machines.

You can use pip freeze > requirements.txt command to create a requirements file which lists all the packages installed in your current environment. And then you can use pip install -r requirements.txt to install the same packages in different environments.

Leave a Reply