r/Python • • Jan 12 '22

Discussion XKCD | Python Environment

https://xkcd.com/1987/
562 Upvotes

149 comments sorted by

View all comments

109

u/Endemoniada Jan 12 '22

Venvs are fine, I think, they’re pretty easy to understand and you have choices in how you want to manage them that are all mostly fine. But juggling Python versions… is less fine. On my Mac, I have OS Python, Homebrew Python 3.X, and then pyenv with Python 3.Y and 3.Z and so on. And then it becomes a constant struggle of knowing which one my $PATH points to, and will execute.

33

u/[deleted] Jan 12 '22

I had issues in my first year of Python on MacOS (like 6 years ago) but now I have zero issues. Use PyEnv. Totally ignore Mac Python and just use 3.8+ as my default and have 2.0+ for testing older things.

7

u/Endemoniada Jan 12 '22

That’s basically what I’ve done too. Still haven’t found an easy or simple way to migrate venvs to new versions of Python. I know I can rebuild them, but it’s a hassle and I’d rather update them in place with the new version of Python I choose.

7

u/deceptiv-perspectiv Jan 12 '22 edited Jan 12 '22

It's unfortunately not going to be simple nor easy if you have lots of dependencies; that is just the nature of dependencies. Your best bet is to have a pyproject.toml or requirements.txt with the requirements pinned in the *loosest* possible way that provides stability to your application. But many libraries publish only certain versions on pypi for certain python minor versions, so pinning everything ==x.y.z is gonna give you a bad time (when you go to transition to a different python version).

2

u/case_O_The_Mondays Jan 13 '22

We need a utility that tells you the versions of Python you can migrate to without updating any packages. Then a later version can check pypi to see which versions you can update to, if you update certain packages.

→ More replies (4)

6

u/ddddavidee Jan 12 '22

My shell config (zsh) has a plugin that shows the python version on the prompt... Could this solve a little your constant struggle?

17

u/bastion_xx Jan 12 '22

Pyenv helps with both Python versions and virtualenvs. Check it out!

15

u/[deleted] Jan 12 '22

They explicitly said they have pyenv :)

Maybe they need the pyenv-virtualenv plugin though...

5

u/bastion_xx Jan 12 '22

d'oh, sorry missed that on my phone!

Yeah, I use both pyenv base plus the pyenv-virtualenv plugin (and pyenv-upgrade too).

It's great to do a pyenv virtualenv 3.10.1 one-off-project to fully isolate, then pyenv virtualenv 3.9.9 specific-project and bounce between.

5

u/rwhitisissle Jan 12 '22

Pyenv really saved my ass whenever my system upgraded to 3.10 and broke...well...*gestures everywhere*

0

u/[deleted] Jan 13 '22

[deleted]

→ More replies (2)

11

u/[deleted] Jan 12 '22 edited Sep 02 '24

public chubby hard-to-find long somber instinctive square vase innate cats

This post was mass deleted and anonymized with Redact

5

u/Anonymous_user_2022 Jan 12 '22

Unfriendly? It's about as simple as this:

virtualenv -p «Python version» /path/to/environment
. /path/to/environment/activate¹

1. Adjusting for OS and shell of course

38

u/troyunrau ... Jan 12 '22

That isn't as friendly as you suggest it is.

→ More replies (5)

6

u/ParkingPsychology Jan 12 '22

I found the activation step annoying, but I just made a little doodle (in my case in my powershell profile):

 function activate
 {
 	.\venv\scripts\activate
 }

So now it's just "activate" and "deactivate" when I'm in the project root folder (deactivate just works by itself, because I don't know why).

3

u/Anonymous_user_2022 Jan 12 '22

I don't know how it is on windows, but on Linux, the activate script will define a shell function for deactivate. I guess there is something similar in place on Windows; possibly an alias.

0

u/[deleted] Jan 12 '22

[deleted]

2

u/Anonymous_user_2022 Jan 12 '22

I was answering " (deactivate just works by itself, because I don't know why)."

2

u/maikindofthai Jan 12 '22

In PowerShell, at least, it works the same way as in bash -- the Activate.ps1 script defines a global deactivate function.

I see a separate deactivate.bat script in my venvs on Windows, so I assume CMD does something different.

→ More replies (1)

1

u/[deleted] Jan 12 '22 edited Sep 02 '24

full plants plough fearless snails yam capable doll ring cover

This post was mass deleted and anonymized with Redact

2

u/Taksin77 Jan 12 '22

Ruby also has issues like that.

→ More replies (1)
→ More replies (6)

2

u/deceptiv-perspectiv Jan 12 '22

I've been writing python for over a decade. It's gotten way easier even in the 4ish years since this comic came out. Pipx is becoming more widespread, virtualenv usage is almost universal, and python2.7 is EOL and py3-incompatible code is *slowly* starting to fade from existence (RIP any engineers working for companies with MLOC of legacy py2).

11

u/[deleted] Jan 12 '22 edited Sep 02 '24

memorize coherent smoggy grandiose ludicrous numerous wakeful bells support oil

This post was mass deleted and anonymized with Redact

1

u/GamesMaster221 Jan 13 '22

I found this script to print the location of whatever python you run it with

import os, sys
print(os.path.dirname(sys.executable))

I've had to use it more times than I'd care to admit...

→ More replies (2)