r/Python • • Mar 28 '23

Tutorial Relieving your Python packaging pain

https://bitecode.substack.com/p/relieving-your-python-packaging-pain
1 Upvotes

36 comments sorted by

5

u/KingsmanVince pip install girlfriend Mar 28 '23

Use “conda”, and only “conda”, for everything.

Do not use “pip” or “venv”. Certainly never mix “conda” with it.

I agree with this. Also use minconda only (or mamba if you want fast things), you don't need a bulky Anaconda GUI, people! For me, I only use conda when I don't want to mess with c/c++/cuda packages such as cuda toolkit, cudnn, ...

6

u/Lomag Mar 28 '23 edited Mar 28 '23

Some context for those who didn't read the article: The article does not recommend using conda--these quotes are from a section titled "What if you are forced to use Anaconda?".

0

u/alicedu06 Mar 28 '23

Well, what do you know, "girlfriend" is already taken on pypi:

https://pypi.org/project/girlfriend/

Would have made a killer djangogirl toolkit

1

u/KingsmanVince pip install girlfriend Mar 28 '23

I can't even install the package, lol

3

u/eigenein Mar 28 '23

You can't have a girlfriend then, sorry

2

u/alicedu06 Mar 30 '23

I don't want to spam post, so I won't create another topic. But the article has a follow up one explaining why it gives those advises:

https://bitecode.substack.com/p/why-not-tell-people-to-simply-use

Some of it addresses points in some comments in this thread.

2

u/i_kant_spal Mar 28 '23

I worked with conda, then poetry. Now, I use pyenv for managing virtual environments. So far, no issues.

2

u/Rawing7 Mar 29 '23

I'm conflicted about this article. On one hand I agree with pretty much everything, but on the other hand I have no idea what "packaging pain" it's trying to solve. I've always done what this guide suggests and never had any problems, so I guess it's correct?

3

u/bjorneylol Mar 28 '23

Great suggestions and a horrible article. Every point is followed up with a bunch of sassy quips with no explanation as to why, or worse "subscribe to our blog and maybe we will complete this article at an unspecified later date"

For everyone else reading:

Don’t install the latest major version of Python

Real reason: many packages don't support the latest major python release right out of the gate - As of last time I checked, Numba doesn't work on 3.11, nor does many of the new Torch 2.0 features. Waiting a few months to upgrade will likely alleviate a lot of headaches as far as package compatibility, IDE support, etc. is concerned

Use only the python.org installer on Windows and Mac, or official repositories on Linux.

This is the most secure way to install python, and the installation from different sources do slightly different things with the system paths - If you trust 3rd party sources and are aware of the differences between them and the official installer this is a non-issue. Ironically the article advocates adding an unofficial PPA on linux to install different python versions - contradict yourself much

Never install or run anything outside of a virtual environment

If you write program 'A' outside of a virtual environment, and then start working on program 'B', installing dependency 'XYZ' may update package 'EFG' to a version program 'A' does not support. Virtual environments ensure all of your programs are isolated and making changes to one environment doesn't break something in a totally different project

Limit yourself to the basics: “pip” and “venv”

Part of the reason why the python packing ecosystem is so fractured and confusing is because every time someone has a slight issue with the built-in components they decide they need to build out a brand new packaging ecosystem - if everyone knew how to use pip and venv properly, then they likely wouldn't have any issues with them. However, everyone reads some 8 year old blog article telling everyone they NEED to use conda/virtualenvwrapper/etc to address some shortcoming with pip that no longer exists, which means these people never learn how to use pip/venv properly, and the cycle proliferates. You are free to convince me of the need to use "tool xyz" because of say, better dependency resolution, but at the end of the day the time lost learning said new tool is going to be much greater than the grand total of 3 minutes I have lost to manually resolving dependencies in the 10 years I've been developing with python

If you run a command, use “-m”

Theory: this ensures you are running the command that is associated with the specific version of python you are using. venv/bin/python -m pip will run the pip version installed to that specific virtualenv

Practice: This is no different than directly calling the binary, e.g. venv/bin/pip or doing a source venv/bin/activate when you enter your project directory, which lets you call the pip directly to access the relevant binary.

1

u/SittingWave Mar 28 '23

Most of what it is recommending does not make sense at all, or it's plain wrong.

1

u/desmoulinmichel Mar 28 '23

Which ones?

2

u/SittingWave Mar 28 '23

all of them. the blog author is pretty much recommending NOT to use those tools that have been made exactly with the specific purpose of mitigating the many, many, many issues related to package management.

It's the software development equivalent of telling you "just get some rest and pray it goes away" when you have a viral infection.

1

u/Rawing7 Mar 29 '23

What issues? I've always done what this guide suggests, and never had any issues.

1

u/SittingWave Mar 29 '23

There's plenty of literature on the web, and I am not going to repeat it. The fact that you never had any issues always point at one or more of three options:

  1. you were lucky

  2. your system is very simple

  3. you never realised you had a problem

0

u/eigenein Mar 28 '23

Some of them actually make the pain even worse, not relieved

2

u/desmoulinmichel Mar 28 '23

Which ones?

2

u/eigenein Mar 28 '23 edited Mar 28 '23

Use only the python.org installer on Windows and Mac, or official repositories on Linux.

Speaking from the MacOS perspective:

  1. Completely deleting such a Python installation involves additional steps to kill the framework and symbolic links

  2. Upgrading minor versions becomes manual, and it's quite tedious if one has multiple Python versions. Eventually, one ends up with outdated ones

  3. One has to set the PATH to each of the interpreters (well, once yet adds routine)

To install things, use pip, and only pip

This is no better than «don't use Python or C, use only bare Assembler»:

  1. Upgrading frozen dependencies becomes a manual task, and people don't like tedious tasks, so some of the deps will stay outdated

  2. requirements.txt usually only contains direct dependencies. Without freezing 2nd-order deps with something like pip-tools or poetry one of those deps of deps will eventually screw up your day when bumped implicitly to a buggy/broken version

So you have two choices. Don't use "-m", and suffer a lot. Use "-m", and suffer a little.

After 10 years, I've freaking no idea what «suffer a lot» they're talking about. With -m one will certainly suffer a little more

Limit yourself to the basics: “pip” and “venv”

I'm not sure here but if they're against make either, then this adds a huge amount of pain by not automating things

2

u/someotherstufforhmm Mar 28 '23

the article didn’t say anything about requirements.txt, did it?

Also, not sure why you have a path for each python, it dumps a versioned executable in bin quite reliably.

This article is exactly what I do, and everyone at my job on a team where we need to develop for multiple versions of python - though a few do use asdf for getting diff versions of python, but that’s fine.

Re: make, I don’t think anyone really opposes make - it’s been used to add steps/run targets to packaging forever, and it can just use pip.

I don’t get the hate for -m either, though I will say re:commands/his final step some of his -m examples ignore packages that have a console_scripts option but don’t expose a main - for which I prefer running directly from the bin, I never activate venvs, just run the python executable in them.

I’m also not sure why the whole -m thing or fun paths scares people. Been doing linux professionally for 12 years, using it for longer, and releasing the fear of scary commands and embracing tapping tab with your pinky to speed up typing long paths is key.

I also don’t really care if people prefer other ways as long as at work they’re not becoming unicorns, I’m a big believer in team uniformity, even if the team chooses a system that isn’t my fave.

1

u/eigenein Mar 28 '23

the article didn’t say anything about requirements.txt, did it?

It did say though about using only pip, I'm not sure not using requirements.txt, setup.py, or pyproject.toml to list dependencies makes any sense – they still usually contain only direct dependencies (unless pip-tools or some sort of make requirements.txt is used)

Also, not sure why you have a path for each python, it dumps a versioned executable in bin quite reliably.

Not on MacOS, there it's a protected directory. On MacOS one has to add all /Library/Frameworks/Python.framework/Versions/3.*/bin to the path

I will say re:commands/his final step some of his -m examples ignore packages that have a console_scripts option but don’t expose a main - for which

Good point, by the way

I also don’t really care if people prefer other ways as long as at work they’re not becoming unicorns, I’m a big believer in team uniformity, even if the team chooses a system that isn’t my fave.

Yeah, fine by me! The OP's article sounds just so… assertive that I couldn't ignore it

-1

u/someotherstufforhmm Mar 28 '23

Oh, so you don’t understand pip - according to your first statement anyways.

Requirements.txt is more of a statefile, vs setup.cfg/pp.toml which is a min-to-install type, they both have different purposes.

I agree most people shouldn’t be using requirements.txt. It’s valuable for reproducible deploys, but really shouldn’t be done in isolation and definitely shouldn’t be used locally. We use it at work to preserve a deploys state.

A deploy involves generating requirements.txt, along with hashes for each dependency, from setup.cfg/pp.toml. We also have weekly dependabot stuff checking the cfg/toml.

All that would be way too much for a local package, for local dev we simply use setup.cfg/pp.toml and only list the min things we need - as intended. It makes local dev simple, but also means for any deploy we can reproduce exactly what happened if it needs re-pushing or recreation, meaning lower non-dev teams can use that requirements.txt sitting on the server to “reset” a server (statefil).

As for the rest of your stuff, I use macOS and each version creates a pythonX.Y file in the same bin directory.

My flow is to create a venv by writing python3.9 -m venv folder/blah, then run things from then on via folder/blah/python or folder/blah/python -m pip.

It doesn’t take me that much time, nor does it take newbies who ask how I work (happens at work a lot, I mentor a few people) that much time once I teach them to tap tab, and there is never any confusion about which venv is running what.

It’s an excellent method that also supports linux CLI skill building, as that’s not involved at all lol. We deploy on hundreds of linux hosts minimum for each app, so I expect them to be able to do that at speed seeing as they’ll also be expected to fluently hop around and search logs, involving way longer paths.

Like I said the only thing I disagree with in his post is the way he puts the -m thing. I do support using -m for pip as it means if someone is activating their venvs and using the PATHd python/pip it guarantees the pip is the correct one in case people did some path wonkiness- which they shouldn’t have, but as the guy who helps newbies, they sure do love putting weird shit that shouldn’t be in their path in their path and not checking it ever, so meh.

1

u/eigenein Mar 28 '23

Oh, so you don’t understand pip - according to your first statement anyways.

That's pretty aggressive, you know? We're discussing the specific article, and re-writing «use only pip» section the right way never was my goal (and also my remark regarding pip-tools and make requirements.txt should've hinted at that, but who reads further anyway?). Hence, the rest is nothing new to me, despite the speedy conclusion about my understanding

As for the rest of your stuff, I use macOS and each version creates a pythonX.Y file in the same bin directory.

Okay, you got me here. In the past, I had to modify the PATH – for whatever reason, never mind. Still upgrading via the installers and cleaning up the frameworks and symbolic links hurts way more without Homebrew

0

u/someotherstufforhmm Mar 28 '23

aggressive

It was just the way you said requirements.txt is an automatic use, when most pip users consider it antiquated and recommend against it, it’s far from automatic. My take on using it as a state file is very rare. same with your comments on setup.cfg, they just tell me you don’t actually know much about pip

It’s not aggressive, just commenting. I don’t assign value. I don’t use poetry or like it, but tons of people here do, I don’t really care what people use as long as they learn it - you’ve openly said you don’t prefer vanilla pip, which is fine so I wouldn’t expect you to know it.

I’m not sure why you’re annoyed or sensitive, you said a few outright incorrect things I was correcting, what part of me saying ah you don’t know pip is aggressive or insulting?

Seeing as the context was you criticizing an article which - albeit relatively reductive and I took some issue with - actually stands out in the sea of things bashing pip without actually getting it, I stand by what I wrote. I don’t get mad when someone points out I don’t know much about poetry - I openly stay vanilla piptools based on exacting work standards supported by industry experts (our python team is semi incredible and spends a lot of time on standardizing packaging / approach).

0

u/eigenein Mar 28 '23

I’m not sure why you’re annoyed or sensitive, you said a few outright incorrect things I was correcting, what part of me saying ah you don’t know pip is aggressive or insulting?

Got judged too fast based on too few things said by me – that's why

Anyway, of course, one can use the vanilla pip (that is, without mere pip-tools) but I don't like the assertive tone of the article – no, it wouldn't relieve the pain – it'd make it worse – unless there's already an existing way of working in the team/department or for personal projects. I could start pretty quickly with either vanilla pip, or Poetry, or whatever – because I've been there, but there should be some assumptions made regarding that before saying «do not use X to relieve the pain»

→ More replies (0)

0

u/desmoulinmichel Mar 28 '23

Pretty much my own worflow, but I use pip tools on top of that.

I tried poetry for years. Always failed at point.

And I code python professionnally, can't imagine what a math teacher does when they get PATH errors.

Nice trick the -m

2

u/SittingWave Mar 28 '23

Nice trick the -m

There is a reason why we have entry points. -m might work, or might not. assuming it always works is hopeless.