Setting Up Python Environments for Different Projects
Tags: python, webdev, programming
Published: 2023-07-02 23:00:52


An environment in Python is a directory, that contains the Python Standard Library, the 3rd party packages. The environment helps us in organizing the project’s specific dependencies and separate from other projects or system-wide installations. There are two types of environments

- System Environment: It’s the default environment which is global, and it is created when we install python.

- Virtual Environment: A virtual environment is a separate environment, a directory that is isolated from the system environment and the libraries installed inside virtual environment will not affect system environment.


![Virtual Environment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/riagnbmeo4sp81n1st9r.png)


There are several reasons to have our own virtual environment in Python.

**Dependency Management and Isolation:** As showcased in the figure above, Environments helps in managing different versions of dependencies, python version for our projects. This helps us to remain compatible while switching between projects.

It also helps in having the isolated environment changes made in one environment will not affect other environment as well as the system environment. It remains completely isolated.

**Collaboration**: Collaboration becomes lot easier by sharing the environment configuration file like ‘requirements.txt’, others can setup the same environment and configurations on their machines with same dependencies and their versions.

Python provides several tools for creating, managing environments we can see some of them while trying to create project from PyCharm and environments can also be created using command-line.


![Pycharm Create Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hexup8d95l6y3ep0j16x.png)

Let’s understand by creating two different environment’s one using Virtualenv and other using Conda, as explained earlier environments are directory, we need to activate them once the environment is activated, we can install packages / libraries either using “pip install” or simplify using installing through “requirements.txt”.

Both the environments that we create has following packages and versions.


![Projects with different dependencies](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3x6o20rkta94d96j7fj.png)

**venv-project**: Let’s create the project named ‘venv-project’ using Virtualenv tool from PyCharm, in this project the version of Python used is 3.8. Once the project is created, we can activate the environment from PyCharm terminal.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/byqft8qa8hge588mlpdv.png)

Once the project is created and activated, packages and its versions can be defined inside ‘requirements.txt’ file and then install them using command ‘pip install -r requirements.txt’


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4aqd1x82270f569jwgrd.png)


Once the packages are installed, we can check the installed version using command ‘pip show package-name’


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39y9w5ck33piiedqogdb.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/skfnn1xz53dpinft18fv.png)

The above screenshots reflect the version of ‘requests’ and ‘sqlalchemy’ installed in ‘venv-project’.

**conda-project**: Now create the project named ‘conda-project’ using Conda tool from PyCharm, in this project the version of Python used is 3.10. Once the project is created, we can activate the environment from PyCharm terminal.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qrlo8r3mobhp4o6d3wg.png)

For activating the environments using Conda, following command should be used ‘conda activate env-name.’

Once the packages are installed, we can check the installed version using command ‘pip show package-name’


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/alh6tbfrscqgudgfwjly.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4iemivn6u4q11tpkrqjl.png)

Both venv-project and conda-project are separate environments with different versions of Python, requests and sqlalchemy library.

Thank You for reading.