r/Python • • Dec 18 '18

Python Virtual Environments: Extreme Advertising Edition

Post image
2.1k Upvotes

287 comments sorted by

View all comments

213

u/ase1590 Dec 18 '18

Remember, a virtual environment a day keeps the xkcd superfund site away.

40

u/Eelz_ Dec 18 '18

Real talk though, is there any way to understand/fix what's going on in this comic? I definitely have this happening on my Mac

34

u/ase1590 Dec 18 '18

Start removing pip packages and clean up.

23

u/[deleted] Dec 19 '18

Have you genuinely used python on a Mac? Honestly I feel like if you haven’t there are headaches you can’t imagine otherwise.

I am well disciplined with virtual environments and every now and then some Mac specific thing will throw a spanner in the works

26

u/synae Dec 19 '18

Step zero, install python with brew so you're not polluting the system python. After that you can make whatever mistakes you want with reckless abandon (and learn from them!)

5

u/[deleted] Dec 19 '18

There’s the issue though with python ‘as a framework’ which is needed for matplotlib and such.

4

u/synae Dec 19 '18

Er... I have no idea what this means to be honest. Is matplotlib not executable within a given environment, i.e. with a particular PATH set? Cuz that's basically all you need for apps to use a virtualenv correctly.

3

u/[deleted] Dec 19 '18

No, I can’t remember the details off hand but getting mpl to behave is an enormous PITA unless you do some very unintuitive stuff.

3

u/Mr_Again Dec 19 '18

If you make your virtualenvs with venv it will take care of that. So my setup is to ignore the system python, install 3.6 with brew, and simply venv off that every time. To be fair I still have a conda lurking around somewhere.

1

u/[deleted] Dec 19 '18

Yeah, I am used to using virtualenv and virtualenvwrapper. If using venv fixes this mpl issue maybe I’ll change.

→ More replies (3)

5

u/[deleted] Dec 19 '18

I am sitting on a Mac, my main development system, right now. I program mostly in Python these days and I really have no idea what these headaches you are talking about are. :-)

Of course, I never personally use the system installed Python - I have clean versions of Python 2.7 and 3.4-3.7 installed, and then I create new virtualenvs for every new project or even when I'm just experimenting with a new package.

If you're using Brew to install Python, that's likely your problem there. You want your development versions of Python to be completely independent of your system.

Indeed this rule is true for every system - if there's a system-installed Python, never change it in the slightest. I learned this the hard way, trying to change the system Python on a Linux box almost a decade ago.

(Looking at my notes, about six months ago I had some weird SSL problem with my 3.4 and virtualenv which seems to have resolved now, but aside from that I don't see much problematic on this machine...)

2

u/[deleted] Dec 19 '18

Do you use matplotlib?

3

u/ase1590 Jan 18 '19

Future me here, I was bored over a weekend and generated a pie chart in matplotlib.

literally just made a python 3.7.2 venv, then pip install matplotlib and it installed and generated my simple pie chart fine.

1

u/[deleted] Jan 18 '19

Saved straight to disk or displayed in a window? And which OSX? Maybe mine is just too old. Which virtual environment software do you use?

1

u/ase1590 Jan 18 '19

This was not on Mac. However you shouldn't be using your system's python install.

This was on Arch Linux using Python 3.7.2, using this pie chart example with matplotlib 3.0.2

code runs fine and a graphical window to display the color pie chart pops up just fine.

$ pythom -m venv matplottest
$ cd matplottest
$ source bin/activate
$ pip install matplotlib
$ python testplot.py

1

u/[deleted] Jan 18 '19

Oh right my point was that matplotlib is a pain in the arse on Macs because of tkinter. On Linux it has always been fine for me.

→ More replies (0)

1

u/jasoncm Dec 19 '18

I've used a setup identical to yours for the last few years after making the mistake of replacing the system python on a Linux install. You can still get bit by a nearly pathological case: a configuration script that contains references to scripts using #!env python in most cases. But with a hardcoded /usr/bin/python burried in there for a single subdirectory... hilarity ensues.

That said, it's still the right way to do things.

→ More replies (4)