Dead Simple Python: Virtual Environments and pip

Jason C. McDonald on January 13, 2019

Virtual environments. If you've ever done any meaningful work in Python, you've almost certainly heard of these. Chances are, you've even been told... [Read Full]
markdown guide

Check out pipenv, it so very nicely married pip and venv/virtualenv. I've quite enjoyed that it tracks the requirements for me as I go.


If you're gonna start with Python 3.6+, venv module (built in) is my recommendation, of course only if you will work with 3.6+ version and you're not looking for previous versions.

Good start for this good serie !


Nice one! Could add pip freeze step for requirements.txt creation, it results in better dependency management, as it handles transitive cases too.


Awesome! Quick question tho on sudo pip. I've had encounters before with it.

Why is it wrong,
and what happens if you do do it,
and how do you "undo" such an action?


It's not exactly wrong, it might not do what you are expecting.
People might run pip install requests and fail because of permissions. They might try sudo pip instead of pip install --user.

Sudo pip will install things in the system's python, rather than the user's python.


Good explanation, @jcsvveiga .

The reason I say you should "never" use sudo pip is, on any system maintained by a dedicated package manager, it can easily conflict with the system-maintained packages, which can make a real mess, especially where dependencies are concerned.


Ah! That makes sense. So it would be much better then to do pip install --user than sudo?

Yes, or if you want a ditectory/project dependencies, use a virtual environment


Hello Jason,
I found this article very informative and point-to-point coverage on topics.
Though I still have doubts on she-bang. As you said above if we write !#/usr/bin/env as first line in python script we don't need to call it with 'python' command on terminal. So I tried this by writing '' on Linux but this script is not getting executed without typing 'python' onto the beginning of the terminal command and it gives error : '' is not a command..
So I didn't understand the use of she-bang properly.
Are there any exceptions to these?


That error has nothing to do with Python at all. Running just... going to search your system environment paths for an executable file called, and I can practically guarantee your project folder isn't part of that environment path.

Thus, you need to run...


The . means "current working directory", and then you build the relative path from there.


Just started with Python a week ago and the first advice I came across: pipenv! Combine venv, pip and whatnot in one simple command. It seems to be "the new standard" and until now seems to work flawlessly.


This is excellent! I did something wrong awhile back and ended up in a situation that makes that second xkcd look very familiar. 😬

(Speaking of xkcd, I don't see any attribution for the comics. While I'm not exactly sure what legal attribution compliance with the license looks like, a link to the source material seems like an appropriate minimum.)

This has me excited to try python again, this time following your clear and concise instructions, and taking advantage of venv! Thank you!


So glad you liked the article!

I went ahead and added an attribution at the bottom, thanks. XKCD is so widespread at this point, they get embedded rather commonly anymore (so much so, that Randall just hands out embed links, which I used). That's why I overlooked attribution. It's polite, anyhow. ;)


Great article Jason, i love how easy is to understand virtualenvs using this.
Recently i came across a problem trying to teach a colleague how to set up his own virtualenv to simulate what i had done with mine since we were trying to mimic the entire environment of mine we faced a struggle and ended up failing :(

A day later we came across pipenv wich reproduced the exact ambient for us and problem solved... now i have to ask whats your stance in this virtualenv vs pipenv that seems to be happening around the python world?

and also are you planning to add it to your dead simple series? i will love to use it to teach my colleagues at college, i love the style of your writing (fan girl screaming on the background).

greetings and lots of tacos for you.


Honestly, I never planned on adding pipenv to this, but I may have to cave and add it anyway! Everyone keeps asking. :P

So glad you enjoyed it!


This has some quality content. Thanks for explaining this. Starts to put things together a little more for me


Thank you for this introductory post to begin the series. I look forward to going through the rest as they are released.


Hi, nice article, but honestly I've abandoned virtualenv in favor of Docker. What are your thoughts about this tool?


Docker has a lot of uses, but merely as a replacement for virtual environments? It's overkill. If a virtualenv is a sandbox, Docker is a clean room. You can use it, naturally, but if the only thing you were needing was a controlled set of Python packages, it's a lot of wasted time and disk space.

Now, if you actually need to control your environment beyond what virtualenv allows you to control, Docker is fine. (It makes sense for web development, for example.)

Just not as a straight virtualenv replacement.


Thanks for your reply, Jason.

I enjoy using Docker, mostly because I always have a database or something similar standing next to the actual application, plus deploying Docker is super easy, but perhaps I should try using venv more. Thank you :)


On what directory should I run the "venv\Scripts\activate.bat" command?


Ah i got it. I should run it in the same directory as venv/ and if i want to run the python i should use the one inside the 'venv\script\python' instead of only typing 'python'.


After countless searching of blogs/websites
Finally found the best and easiest tutorial of virtualenv 😄


I’m very excited for this series! Amazing so far! 😁


That's something I like to read.
Thank you, excellent.


So I already have python2 and python3 on my Mac. How to handle that? Should I remove python2?


You can absolutely leave them both installed. That's part of the beauty of a virtual environment! Just specify which one you want to use when you create the virtualenv. For example...

virtualenv -p python3 venv
code of conduct - report abuse