Creating, Installing and Using Virtual Environment in Python - Python for Data Analytics

Python Programs | Python Tricks | Solution for problems | Data Cleaning | Data Science

Creating, Installing and Using Virtual Environment in Python


virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip.


On macOS and Linux:

python3 -m pip install --user virtualenv

On Windows:
pip install --user virtualenv

Creating virtualenv:

virtualenv allows you to manage separate package installations for different projects. It essentially allows you to create a “virtual” isolated Python installation and install packages into that virtual installation. When you switch projects, you can simply create a new virtual environment and not have to worry about breaking the packages installed in the other environments. It is always recommended to use a virtualenv while developing Python applications.

On macOS and Linux:
python3 -m virtualenv env

On Windows:
virtualenv env

Activating a virtualenv:

Before you can start installing or using packages in your virtualenv you’ll need to activate it. Activating a virtualenv will put the virtualenv-specific python and pip executables into your shell’s PATH.

On macOS and Linux:
source env/bin/activate


On Windows:
.\env\Scripts\activate


**Remember env is the virtual environment you have created**

Leaving the virtualenv:


If you want to switch projects or otherwise leave your virtualenv, simply run:

deactivate

Installing packages:


Now that you’re in your virtualenv you can install packages. Let’s install the excellent Requests library from the Python Package Index (PyPI):

pip install requests

1 comment:


  1. Thanks for sharing amazing information about python .Gain the knowledge and hands-on experience in python online training

    ReplyDelete