Comparing Python Virtual Environment tools

Comparing Python Virtual Environment toolsKaran BhanotBlockedUnblockFollowFollowingFeb 1Photo by rawpixel on UnsplashThanks to Keith Smith, Alexander Mohr, Victor Kirillov and Alain SPAITE for recommending pew, venv and pipenv.

I just love the community that we have on Medium.

I recently published an article on using Virtual Environments for Python projects.

The article was well received and the feedback from readers opened a new view for me.

I wasn’t aware about pew, venv and pipenv previously.

Their recommendation helped me learn about the latest technology in the area and further improved my knowledge and experience.

After their suggestions, I read about all of them.

In this article, I’ll share the new Virtual Environment tools I learnt and how they contrast with one another.

Various tools for Python Virtual EnvironmentsPhoto by Plush Design Studio on UnsplashI browsed through various documentations and articles, and learned about how each one of them brought different things to the table.

My findings are described below:1.

virtualenvI’ve already discussed about virtualenv in my previous article.

You can check it out here.

The guide is available here.

Advantages:It’s easy to upgrade via pip and one can easily work with multiple versions of Python.

It also supports Python 2.

7+.

Disadvantages:Here, the Python interpreter binary is actually copied into a new location and needs to be read from there.

Also, if you want to use this, you’ll have to install it separately as it does not come shipped with Python.

Side note: If you plan to use virtualenv, Rafał bluszcz Zawadzki recommended the use of virtualenvwrapper.

Do check it out.

2.

Python Environment Wrapper (pew)pew acts as a wrapper and you only need to remember this command once you start working with it as it can be used for all commands that we would need.

The full documentation is available here.

Advantages:It makes working with virtual environments very easy.

Using a single command, you can create a new environment, install a list of packages and activate the environment.

Disadvantages:The main problem that I noticed is that its support is very limited now.

The last commit on the code is from March 2018, which is almost a year old.

3.

venvvenv can also be used to set up and access Virtual Environments.

It became the recommended method for creating virtual environments starting Python 3.

5.

You can find its documentation here.

Advantages:It generates a configuration file that is directly understood by the Python binary and does not require copying the binary to a new location.

Furthermore, it is being supported by Python developers.

Disadvantages:It is not for Python versions before 3.

3.

4.

pipenvThis tool takes working with Python and Virtual Environments to the next level as it combines package and environment management support into a single tool.

The docs are here.

Advantages:We can simply specify the environment with which we are working.

Also, it allows us to create separate sections for our environment such as production and test-development.

Disadvantages:It must be downloaded separately via pip.

It adds a lot of functionality but one should adopt this if they feel they would require the extra features too.

According to my experience, all of the various tools solve the problem.

Each of them can help you create a Virtual Environment easily, without much hassle.

You can simply pick any one of them and learn its working.

I identified that pipenv is one of the best tools out there to work with Virtual Environments and is increasingly gaining momentum due to its added functionalities.

Thus, I’ll discuss the basics to get you started with pipenv.

pipenvPhoto by Farhan Azam on UnsplashTo install pipenv using brew, use the following command.

brew install pipenvCreate EnvironmentOnce pipenv is installed, we can start working with our environment.

Go to any folder where you’d like to create the environment.

I will create the environment inside pipenvProject.

Just go inside it and install any package you’d like.

Let’s install requests.

This will automatically set up the environment, create the Pipfile and Pipfile.

lock.

cd pipenvProjectpipenv install requestspipenv install requestsTwo files will be created in the directory.

Pipfile which includes the list of packages, Python version and other information.

A lock file is also generated as Pipfile.

lock.

PipfilePipfile.

lockTo activate this environment, we can simply say pipenv shell.

We are now in the Virtual Environment.

We can now use python command to get into Python and use import requests to check if we have the package.

As we get no error, this means the package is installed.

Inside environmentWe can exit from the environment using exit.

As we exit the environment, we will no longer be able to access requests package.

Outside environmentWe can also run commands directly inside the environment without explicitly calling the shell.

For this, we can use the run command.

So, if you have to run Python, use the following command:pipenv run pythonCompatibility with non-pipenv usersWhenever we are installing a set of packages in Python, we usually specify the list in a file called requirements.

txt.

Then, simply run the command below and install all packages in one go.

pip install -r requirements.

txtSo, when you start working with pipenv, there is a method to create the text for this file.

Simply, use the command:pipenv lock -rRequirements file textIt will display the text for our requirements.

txt file which others can use.

Delete EnvironmentIt’s easy to delete an environment using the pipenv —-rm command.

To create it again, use the command pipenv shell.

However, the files in the directory will still remain as it is.

If you wish to completely remove it, delete the directory.

cd .

rm -r pipenvProjectConclusionIn this article, I discussed the new and advanced tools of Python Virtual Environments, including pew, venv and pipenv.

I also enlisted their advantages and disadvantages.

Bonus:ErrorIf you ever get stuck while creating Virtual Environments and encounter the above error, then there is a simple fix.

Create a file .

bash_profile with the touch command in the main directory.

Then, open it in editor (I am using vim).

touch ~/.

bash_profilevim .

bash_profileAdd the following two lines, save it and restart your terminal.

The problem will get resolved.

export LC_ALL=en_US.

UTF-8export LANG=en_US.

UTF-8Please feel free to share your thoughts and ideas.

I’d love to hear from you.

.

. More details

Leave a Reply