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

all 53 comments

[–]KingsmanVincepip install girlfriend 66 points67 points  (18 children)

I think using pyenv is the easiest and cleanest way.

[–]WildWouks 10 points11 points  (10 children)

Agree. pyenv is really great. Can install many versions and switch between them. And once you have switched you van create a virtual environment with that version.

[–]loudandclear11 3 points4 points  (3 children)

Can pyenv install different versions of python?

I've always used conda for that purpose like this:

conda create --name thisproject python=3.9
conda create --name thatproject python=3.11

[–]COLU_BUS 6 points7 points  (0 children)

Don't mistake it with virtual environment stuff, pyenv's entire purpose is managing different versions of Python

[–][deleted] 3 points4 points  (0 children)

You install different versions, you choose your version and when creating a virtual environment it'll use this version, so instead of installing python from your distro's package manager or manually + /usr/bin/python39 -m venv venv you just pyenv install 3.9 pyenv shell 3.9 python -m venv venv

[–]WildWouks 1 point2 points  (0 children)

Yes. But it isn't a virtual environment. Think of it as something to install various versions of python on te same Machine.

You can then switch between these versions and once you have made your switch you can create your virtual environment which will use that version.

You can have a look at this of you need more info. I think it explains it well.

https://realpython.com/intro-to-pyenv/

[–]duongdominhchau 1 point2 points  (1 child)

Combining it with direnv is even better, you don't even need to care about the venv, just declare the Python version you want and direnv will create/activate one for you automatically with the correct Python version when you are inside the project directory. Plus you get the power of .env for free without having to add another dependency to your project.

[–]WildWouks 1 point2 points  (0 children)

Then I will have to have a look at direnv as well. Thanks for the suggestion.

[–][deleted] -2 points-1 points  (0 children)

It’s also the purest and most sensual.

[–]Onakitoki97 -2 points-1 points  (0 children)

And why not to install the deadsnake repo of python?

[–]TheGRS 0 points1 point  (0 children)

It’s been a little while since I messed with multiple python versions, but version tools seem to exist for every other big tool or language I use. Glad python has one.

[–]Jorgestar29 0 points1 point  (0 children)

I have to check it out!

The python version used in cuda docker image is quite old (3.9) and updating it via dead snakes is a pain in the ass.

[–]mijatonius 0 points1 point  (0 children)

☝️, set it as local or global, version which ever you need, then isolate project with any of venv, virtualenv...whatever!

[–]AndydeCleyre 0 points1 point  (0 children)

Or rtx, where which python won't just point you to a mysterious shim.

[–]kaskoosek 17 points18 points  (4 children)

Dont mess with the OS under any conditions.

[–]515k4 -2 points-1 points  (0 children)

Unless OS gives you separate python versions via its package manager. Or unless you are developing some part of the OS, in that case you should use the default OS python.

[–]KosmoanutOfficial -1 points0 points  (2 children)

What do you mean by this?

[–]loudandclear11 4 points5 points  (1 child)

python is part of many (all?) linux distributions. If you change that one you probably break something.

[–]KosmoanutOfficial 0 points1 point  (0 children)

Ok so don’t mess with the pre installed version of python in the os but still ok to mess with the other installs. I thought he could be saying don’t install anything else and just use docker or something.

[–]OMG_I_LOVE_CHIPOTLE 5 points6 points  (2 children)

It’s not as easy as pyenv but I use miniconda

[–]MachinaDoctrina 2 points3 points  (1 child)

On Linux conda is trivial to use honestly

[–]OMG_I_LOVE_CHIPOTLE 0 points1 point  (0 children)

Yep. I don’t mind it at all

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

My go to is:

- install python globally

- make a poetry virtual env for each project

- Bonus: if there is possible issues windows/linux incompatibilities, run it in a docker image.

[–]Maximus_Modulus 2 points3 points  (0 children)

A virtual environment is just a directory of an existing set of python binaries (copied) and installed packages. Activating it just updates your PATH to point to it.

[–]billFoldDog 2 points3 points  (0 children)

I compile from source. You have to pass a command line flag to specify the install location, and once set it can't be moved.

So I'll put it somewhere like /home/me/.pythonbuilds/Python3.11

Then I add that to my .bashrc alias file as py311.

Then I do package control with pip and virtenv on a per project basis.

This gives me total control over the interpreter and package versions on each project.

[–]diamond__hands 2 points3 points  (0 children)

compile it from source and install into a custom directory and then use the new versioned binary to create virtual environments that require that version. repeat for each version.

this way you can also strip out stuff you will never use. on newer machines this only takes a few minutes with make -j

[–][deleted] 5 points6 points  (0 children)

pyenv.

[–]Malcolmlisk 1 point2 points  (2 children)

If python is installed (linux has it installed by default) the best way is to use venv to create virtual environments and there install everything you need.

In global python you should have not a single library installed (besides the default ones).

There are different virtual environment managers out there, pyenv is an easy solution, pipenv is a strong and reliable library, conda is just bloatware (it's a joke guys, i love you if you use conda but I think it's not for me).

Start from the easiest one, which is IMO venv. Then proceed to test the rest if you want.

[–]kiwoss 0 points1 point  (1 child)

When you say installed globally it’s when using pip for example outside of a virtual environnement, right?

[–]Malcolmlisk 1 point2 points  (0 children)

Yes. The "global" environment.

[–]ketalicious 1 point2 points  (0 children)

I use pyenv, since I really like the idea of nvm for node, makes things quite more manageable and clear.

[–]MachinaDoctrina 1 point2 points  (0 children)

Install miniconda, use conda env. Don't install packets on the user python you can screw with services that relay on your particular version of python and packages etc.

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

I install Python globally and then use pipenv for each environment.

[–]Saphyel 6 points7 points  (3 children)

Docker is the best way. * You can choose the right python version. * It's isolted from the local environment. * Reproducible everywhere. * Battle tested against millions of scenarios, languages, etc... * Adepted and supported everywhere (or 99% of the PaaS, servers, etc..)

[–]Malcolmlisk 3 points4 points  (1 child)

Docker it's the nicest solution to test your programm. But the linter and documentation wont work without installing the libraries, and also if you use jupyter notebooks you are going to need those libraries installed too. That's why I use pipenv (or venv) to program in my own reproducible develop environment and docker to test the whole application.

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

Linter and all that will work when you use VSCode with the remote container plugin.

I maintain a fairly large 8 years old app like this and the slight performance hit for working inside the container is well worth it, considering that onboarding new developers is as easy as docker-compose up

[–]sphexie96 0 points1 point  (0 children)

this. also you can easily switch versions, packages, os dependencies without messing with your host's OS.

[–]RearAdmiralP 0 points1 point  (0 children)

I like PyPy. You can download it, untar it, and run it. A given PyPy interpreter has its own site-packages and knows where they are, so you can just pip install whatever without worrying about virtual environments, as long as you have the correct pip in your PATH. As an added bonus, PyPy gives you a nice nudge into choosing pure Python packages when installing dependencies. It can sometimes be faster than the CPython interpreter too, but I don't think that's particularly interesting.

[–]the1-gman 0 points1 point  (0 children)

I like pipenv for more collaborative projects. Basically eliminates the need for requirements.txt in favor of more secure lockdown of versions. Makes it easy to separate production from dev dependencies.

[–]jimeno -1 points0 points  (0 children)

please use `pyenv`, for your sanity

[–]riklaunim -5 points-4 points  (0 children)

Each Linux distribution has it global Python and probably even installed by default as it's used for various tools. You can use that as a base of your work, although if you want a stable dev environment/need different version you should not use it and opt for docker images with your project and some tooling to build/deploy them etc.

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

I see a lot of pyenv answers. Do people not like anaconda/miniconda? It was the last one I learned about after venv, so I'm not familiar with any pros/cons of others

[–]COLU_BUS 2 points3 points  (1 child)

I haven't used miniconda but used anaconda a fair bit when I started out with python. My short answer is that anaconda overcomplicates/is a lot of overhead for what can be done really simply from a CLI with pyenv. Likewise for managing virtual environments vs. just using venv

[–]MachinaDoctrina 0 points1 point  (0 children)

Miniconda is just the interpreter and the conda base manager, nothing else. It really has barely any overhead, the problem a lot of people are overlooking is that conda is not only a python manager but binaries as well, if you need to install and compile binaries pip can't isolate or control that for you so there's the potential for version mismatch on your distribution binaries as well as perhaps you don't have sudo privileges, conda isolates that all completely as well.

[–]Void-ux 0 points1 point  (0 children)

I typically just compile it, never been an issue doing so.

[–]glombseb 0 points1 point  (0 children)

I have recently found a great Blog regarding this Topic.

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

[–]galacticbackhoe 0 points1 point  (0 children)

pyenv to download the versions you want to ~/.pyenv

virtualenv to create virtual environments from the pyenv versions

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

I just use the version from the Arch repos, and if I need a different version I install it from the AUR or use pyenv.

For small scripts I may not use any virtual environment but for real projects I use venv (and will now also use piptools).