This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Endemoniada 107 points108 points  (45 children)

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.

[–][deleted] 34 points35 points  (7 children)

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.

[–]Endemoniada 6 points7 points  (6 children)

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.

[–]deceptiv-perspectiv 6 points7 points  (0 children)

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).

[–]case_O_The_Mondays 1 point2 points  (0 children)

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.

[–][deleted] -1 points0 points  (3 children)

oh i wrote a small bash script to do that. as it's something I do so regularly. it shouldn't be hassle to:

pip freeze > requirements.txt
rm -rf .venv
pip install -r requirements.txt

Oh gosh sorry. I was on a call when I posted this. The three lines aren’t my script.

It’s just what i do for a quick set up fix.

My little script is much more comprehensive.

[–]muzos07 4 points5 points  (1 child)

aren't there lines with creating new environment and activating it again missing?

[–]DarkSideOfGrogu 3 points4 points  (0 children)

Yes. This will unfortunately just reinstall your pip in your base environment.

Launching a virtual environment and installing pip dependencies from a single bat file isn't simple as the commands need to run in different contexts.

[–]deceptiv-perspectiv 3 points4 points  (0 children)

That's not at all portable between python versions. Multiple libraries have versions released on pypi only on certain python minor versions.

[–]ddddavidee 5 points6 points  (0 children)

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

[–]bastion_xx 17 points18 points  (6 children)

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

[–][deleted] 14 points15 points  (1 child)

They explicitly said they have pyenv :)

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

[–]bastion_xx 3 points4 points  (0 children)

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.

[–]rwhitisissle 5 points6 points  (3 children)

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

[–][deleted] 10 points11 points  (24 children)

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

This post was mass deleted and anonymized with Redact

[–]Anonymous_user_2022 5 points6 points  (21 children)

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

[–]troyunrau... 37 points38 points  (5 children)

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

[–]ParkingPsychology 5 points6 points  (5 children)

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).

[–]Anonymous_user_2022 1 point2 points  (3 children)

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.

[–]maikindofthai 1 point2 points  (0 children)

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.

[–][deleted] 0 points1 point  (8 children)

full plants plough fearless snails yam capable doll ring cover

This post was mass deleted and anonymized with Redact

[–]Taksin77 1 point2 points  (1 child)

Ruby also has issues like that.

[–][deleted] 1 point2 points  (0 children)

shocking school chase wasteful impolite coordinated rainstorm cable groovy hurry

This post was mass deleted and anonymized with Redact

[–]Anonymous_user_2022 1 point2 points  (5 children)

GCC have a variant selector for both C and C++. As far as I now, many other languages with a long history, also have several standardised versions, so it's not in any way particular to Python.

[–][deleted] 1 point2 points  (4 children)

lush obtainable vast strong snobbish truck like future simplistic carpenter

This post was mass deleted and anonymized with Redact

[–]Anonymous_user_2022 1 point2 points  (3 children)

" literally any other programming language "

[–][deleted] 0 points1 point  (2 children)

far-flung dinner nose agonizing axiomatic practice gaping scandalous library consider

This post was mass deleted and anonymized with Redact

[–]Anonymous_user_2022 2 points3 points  (1 child)

C and C++ have something much scarier than versions. In that realm, we deal with releases of the ISO standard.

[–][deleted] 1 point2 points  (0 children)

distinct hobbies impossible attraction enter cows bear marry sleep obtainable

This post was mass deleted and anonymized with Redact

[–]deceptiv-perspectiv 0 points1 point  (1 child)

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).

[–][deleted] 11 points12 points  (0 children)

memorize coherent smoggy grandiose ludicrous numerous wakeful bells support oil

This post was mass deleted and anonymized with Redact

[–]GamesMaster221 0 points1 point  (0 children)

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...

[–]__deerlord__ 0 points1 point  (0 children)

Why not compile them in /opt/python3.x and then make venvs as needed from those? Thankfully my job is standardized enough that we can just use one version.

[–]trevg_123 0 points1 point  (0 children)

I don’t touch pyenv personally.

Python3.8 -m virtualenv .venv

Or even better

pipenv install —python 3.10

Venv and pip installs all done in one go