9 ms·
Managing Python Versions with Pyenv
- huskyr 4y agoOne thing i like about pyenv that isn't very well documented is the fact that you can easily install the latest release from a specific version by using the :latest suffix. For example: pyenv install 3.8:latest Will install the latest 3.8 Python version.
- claytonjy 4y agowow you're right, that should be documented, I'm going to use this every time now. I've always been annoyed at having to learn what the latest patch versions are.
- lazypenguin 4y agoI think you collectively saved hundreds of pyenv users from some minor suffering! Thanks for the tip!
- fock 4y agoAnd yet I wonder why this is supposed to be an integral part of the Python ecosystem. Q: Now you want to build your Fortran extension with a compiler supporting OpenACC. What now? This should show relatively clear that this tool is just an edgecase of a quite general problem. I recommend a general solution (like spack).
- nerdponx 4y agoBecause a lot of projects, probably the great majority of projects, do not need this level of control. If you need that level of control, use Spack or Nix or whatever. Whenever threads like this come up, people have a weird tendency to hold Python on some kind of pedestal above other languages. I can't think of a single language package manager or installer or version manager that makes even a faint attempt to control system level dependencies like a Fortran compiler. Why is Python somehow bad for failing to manage this? The problem is and should remain out of scope for any particular language tool chain.
- fock 4y agoYes, I agree on that, spack can lock everything except the kernel and libc. I also don't think this is a failure of "python" as some others have raised that in the vincinity. Managing binaries is just out-of-scope and should be left to another tool - such as given with pyenv. It's solving this problem in a narrow way, which most commenters find confusing and I find a bit useless compared to alternatives!
- disgruntledphd2 4y agoThe reason that Python gets held to this standard is because data science/analytics etc has lots of C and Fortran dependencies and this interacts terribly with the YOLO attitude of python dependency management tools (pip did not check for dependency compatibility until 2020, and it still doesn't keep your environment consistent.
- blueflow 4y agoIs this something distinct from pipenv? Gee, python packaging is a mess..
- dygd 4y agoPyenv is a bit lower-level and works well with requirements.txt. Pipenv can use pyenv to install Python venvs and brings its own Pipenv and lock files.
- samwillis 4y agoI believe pipenv is a wrapper around pip and pyenv. Pyenv is brilliant, defiantly recumbent it as the standard way to manage python versions. It's basically nvm but for python.
- quacker 4y agoPyenv has nothing to do with pipenv. Pyenv is mainly for installing versions of Python. It's useful if you work on multiple packages that each needs a different version of Python, or if you have one package that you test with multiple versions of Python. (Alternative to apt/yum/brew or manually downloading Python from python.org) pipenv is for installing Python packages for your project. (Alternative to pip, poetry, etc)
- marban 4y agoPyenv combined with Poetry is still the cleanest way to run multiples envs. Any other suggestions?
- wdroz 4y agominiconda is nice for that too, especially if you want non-python things like cuda.
- yawgmoth 4y agoI had a lot of fun with https://pdm.fming.dev/ https://pdm.fming.dev/
- marban 4y agoNice, never heard but almost seems like a 1:1 competitor?
- vajenetehais 4y agoI've always found pyenv a bit complex to deal with. I've also encountered some bugs with a fresh debian install and pyenv. To put this in perspective, i'm not the sharpest knife in the drawer (not a programmer, not an adminsys). I prefered alt install for a specific version (python3.3, python 3.X, etc. with associated pip) and virtualenv. Then doing `python3.X -m venv` i got what i need. VScode or pycharm can bind to it. Recently i've tryed Fedora and they ship python versions compiled so you just need to use the package manager. It's easier and it deals with updates. I stick with that now (i'm migrating from deb to fedora for my servers).
- tempest_ 4y agoI like and use poetry, it generally just works. It is however slow as molasses and its error messages are sometimes worse than a stack trace.
- ellisv 4y agoI use pyenv + direnv. If my project uses 3.10.3 I just add `layout pyenv 3.10.3` to my .envrc file. This creates a venv in the `.direnv` subdirectory. When I `cd` into the dir my environment is automatically configured to use the virtual environment and when I `cd` out the configuration is removed. If you want to use poetry + direnv, that's also an option. https://github.com/direnv/direnv/wiki/Python#pyenv https://github.com/direnv/direnv/wiki/Python#pyenv https://github.com/direnv/direnv/wiki/Python#poetry https://github.com/direnv/direnv/wiki/Python#poetry
- trquantum 4y agoWhat is with the endless stream of basic Python articles? Perhaps the Python ecosystem is so complex that it requires constant education.
- marban 4y agoIt's like CSS — You unlearn it quickly if you don't train the Python muscle.
- catchclose8919 4y agoIt's a mess... there is no convergence on package managers or anything and everything sucks. Node/nvm at least was forced to fix its s by how bad it originally was. In Python everything sort of almost works good enough for each use case, so half thought solutions keep staying in each niche...
- samwillis 4y agoI highly recommend Pyenv, it's the best way to manage Python versions. My workflow on a Mac is to use Brew to install Pyenv and then Pyenv to install Python versions. Similarly I use Brew to install NVM and then NVM to install and manage Node versions. For python virtual environments I go with vanilla `python -m venv env`, it works well and I like that the `env` directory is under the project I'm working on.
- rubyist5eva 4y ago> My workflow on a Mac is to use Brew to install Pyenv and then Pyenv to install Python versions. Similarly I use Brew to install NVM and then NVM to install and manage Node versions. Might I suggest asdf (asdf-vm.com). I use it for managing ruby, node, go and python versions and it has been rock-solid. You can also install it from homebrew.
- volker48 4y agoI switched from installing Python with Homebrew to using asdf and I've been very pleased with the results. With Homebrew, every significant macOS update would break all my virtual environments. With asdf this is no longer an issue. The ability to set local versions of Python for different projects is very nice as well.
- alex_suzuki 4y agoWhat’s wrong with running `python3 -m venv venv` and then activating/deactivating the venv as you go? Feels like if there’s an out-of-the-box way for keeping separate environments with scoped dependencies, then we shouldn’t fragment things further with yet another approach?
- Bellyache5 4y agoThe wrinkle comes in when you need different versions of Python for different projects. e.g. Python 3.6 for one, 3.10 for another.
- icedchai 4y agoSo what’s the actual problem? You can install multiple python versions and use the correct one when creating the venv.
- rodelrod 4y agopyenv helps you install and manage those multiple python versions.
- criddell 4y agoI don't use pyenv and have multiple python versions installed (and use them via venv per project). If you don't need to designate one as a global/system Python (which I don't), is there any reason to use pyenv?
- rodelrod 4y agoIf your process to install a specific Python version for a project, independent of system Python, is as fast and simple as `pyenv install 3.9.7`; and if you don't work in a team where having `.python-version` file in the project will download, install and set the exact Python version required: you don't need pyenv.
- 4y ago
- Nihilartikel 4y agoAsdf provides a very similar workflow, but for many other packages too. I'm really drawn to Nix for it's power and purity, but until it has a first class way to install specific older software versions the 'worse is better' asdf way wins.
- deleted 4y ago[deleted]
- aquova 4y agoMy frustration with managing different Python versions is what motivated me to finally go out and learn Docker, after running into scenarios where I needed to manage different versions of Python with different versions of each library.
- whalesalad 4y agopyenv will do this with the virtualenv integration, so you get the best of both worlds: pyenv virtualenv <python-version> <env-name> pyenv activate <env-name> pip install ...
- sonthonax 4y agoNot really an option on a Mac. Docker is so incredibly slow not only because of the virtualisation layer, but because of file system performance (Python hits a lot of files on startup). It’s also not that complex at the end of the day. You have two things involved in managing python versions: the executable you’re running, and the PYTHONPATH. I actually don’t understand why people find that so complex. A virtual environment is: an alias to a python executable; a folder called ‘site-packages’ which is then added to the PYTHONPATH; and a script to set you PATH and PYTHONPATH.
- samwillis 4y agoI get what you mean but being on a Mac I now avoid Docker for development, it seemed to cut my battery life in half whenever it was running.
- kh1 4y agoI always have different python versions on system level (`$ sudo apt install python3.7 python3.8 python3.9 python3.10`) and use virtualenv to manage versions for each project (`$ virtualenv env -p python3.9`), and that has served me well for years. Could someone tell me why pyenv is superior?
- mpicker0 4y agopyenv can compile Python versions that might not be available with "apt install" yet. And you can have multiple minor versions available (3.7.8 and 3.7.9, for instance), if you ever need it.
- zach_garwood 4y agoI don't know if it's "superior", but it helps you manage / install / uninstall / set system Python versions, as well as virtualenv features. That's it.
- kh1 4y agoThanks. IMO changing system python versions might confuse some people. I often have to debug not-so-tech-savvy colleagues' python scripts/environments. If we used pyenv, I could picture me telling them that they forgot to switch system python environment.
- 0x008 4y agoIf you forget to activate the venv you cannot accidently install packages in your system python.
- jonpalmisc 4y agoProbably going to catch hate for this, but the Python package/environment management debacle is one of the things that causes me to avoid the language in my own personal projects. All of the following are real tools for Python package/environment management: virtualenv; pipenv; venv; pyenv; poetry; pyenv-virtualenv; and pip-tools. There are probably more. Which one is "best" varies by who you ask and with time. If you do a bunch of Python work, you'll probably need most of them installed since different projects use different tools. The messiness of the whole situation just takes the fun out of it for me. I don't hate Python, but when I code in my free time, I'd simply prefer to use a language that doesn't have those problems.
- markburns 4y agoWith libraries that alter your path at runtime. I've encountered comments on issues that implied that this wasn't a complete hack. I have had the impression that this is all just overhead in getting an environment up and running, and not taken for granted that you should be able to achieve repeatable isolation relatively simply. So I think even if one of those packaging tools worked really well, a library somewhere could undo your best efforts at isolation. I also don't hate python itself, but I wish the one way of doing things ethos applied to managing dependencies and python versions.
- fock 4y agoI don't understand what the problem is: 1) you install ONE version of python, more can't coexist in your environment usually 2) then you install your packages in a venv (because python provides tools to configure this) Only part 2 is a problem of the Python ecosystem, part 1 is a problem of any environment to run binary programs. It applies to C-compilers, node, ..., perl... Naturally, as most people involved with Python seemed to agree with that view, there was a lot of room for "improvement". And people got their improvement aplenty. Other people just realized that this is a general problem and nowadays instead of keeping around their /opt they now use something like spack. Which allows you to install a range of python-versions just fine. As well as C-Compilers. As well as perl-versions.
- samwillis 4y ago
- tyingq 4y agoLast I checked, pyenv was dependent on absolute paths that can't change. This makes it very difficult to use with things like CI/CD pipelines that copy repos into temporary directories. There used to be a "--relocatable" switch, but it's gone now. https://github.com/pypa/virtualenv/issues/1549 https://github.com/pypa/virtualenv/issues/1549 Edit: Ah, yep, somehow read this as virtualenv.
- athorax 4y agopyenv != virtualenv though?
- neonihil 4y agoUnpopular opinion: tools other than virtualenv are for people who do not understand how virtualenv works. On the other hand: people who do understand how virtualenv works, know it very well we: don’t need the other tools than virtualenv.
- tandav 4y agoDo you mean venv which is part of standard library or virtualenv (separate library)?
- neonihil 4y agoAFAIK venv is a subset of virtualenv brought into the standard lib around python 3.3. I’m talking about the “full” virtualenv. This one: https://pypi.org/project/virtualenv/ https://pypi.org/project/virtualenv/
- anuragsoni 4y agovirtualenv doesn't let you manage installations of different python versions. This is the problem that pyenv solves.
- fock 4y agoAnd this is not a Python problem either. It's a general problem.
- neonihil 4y agoIMHO it’s solved by installing different python versions using the OS package manager, like `dnf install python3.8 python3.9`, and then creating virtualenv using whichever version you need, like `virtualenv -p python3.9 venv`
- anuragsoni 4y agoYou'll be limited to whatever python version is packaged on the host operating system. pyenv can let you test against development versions of python if needed, or released of python that are either new enough (or old enough in some cases) that they aren't available on your operating system's package repositories.
- daralthus 4y agoNever tried Pyenv, but I can highly recommend Mamba a fully Conda compatible package manager that is insanely fast solving dependencies. Installing python: `mamba install python=3.9`
- ZeroCool2u 4y agoHow does mamba do inside Docker containers? Does it have the same issues that the conda activate command has?
- shrumm 4y agoI use miniconda and have had no major issues. I’m curious if anyone here switched from conda to an alternative and had a better experience.
- athorax 4y agoI switched from conda to pyenv after they changed their license
- omarhaneef 4y agoUpdates work about half the time for me on mini-conda, but they break the other half of the time. When I look up the issue, I am not alone. I end up re-installing it a lot.
- dannyz 4y agoI have had this happen a few times too. I have found the best way to minimize this is to install nothing in the base environment except for conda and make sure you use a different environment for everything else.
- omarhaneef 4y agoI appreciate the time, but I have two responses: The first, that has always been my practice. Secondly, the vast majority of solutions offered by the many people who face similar issues don't work for me or, apparently, for other people reading the solution as well.
- dagw 4y agoI use conda for my data analysis environments and poetry+(pyenv) for developing python libraries and application that are intended for running on machines others than (just) my own.
- ipiz0618 4y agoI switched from conda to poetry because everyone was praising it last year. Then I switched back because it's a pain to switch between OSes using poetry. I only use conda + pip install now, works well enough that I don't have to struggle every time I deploy in a new environment.
- gegtik 4y agoMy personal approach has been to use conda for binary management and a place to stuff libraries (could just as easily use venv here), but the key tool I'm using to pin libraries and avoid transitive dependency drift is pip-compile. Frankly the invocations are so arcane for these various supporting tools that I have been distributing a standard Makefile to projects to hide the complexity - "What's old is new again"!
- senko 4y agoPSA: You don't need to use Pyenv (or asdf, or anything else...) on Linux. It just creates a mess. If you need to install multiple versions of Python that are not available from your distribution packages, simply download the code, install it to a place like /opt and then point your virtualenv to that version when you need it. Here's what's needed: > ./configure --prefix=/opt/python-<VERSION>; make; make install This is not more complicated than what pyenv asks you to do (read the article). Once you have all the versions installed, no need to use a tool to switch between them. When you use virtualenv (which you should, and no need for pipenv, poetry and the rest), just do this to create a new virtualenv: > /opt/python-<VERSION>/bin/python -m venv /path/to/where/you/want/to/store/your/virtualenv This will pick up the correct version of Python. Once you activate the virtual environment, that's it. There's no need to use anything besides what default Python installation already gives you. PS. What about the dependencies? Glad you asked: `apt build-dep python3` on Debian/Ubuntu or `dnf builddep python` on Fedora. No need to go through them manually or copy/paste some list off someone's blog and hope it's still valid. PPS. All this applies to Python 2 with minor tweaks, but hopefully you're not starting new Python2 projects in 2022.
- uranusjr 4y agoThere’s a command in pyenv called “python-build” that more or less automates the Python compilation part. Most don’t realise it (it’s hidden from the general UI) but that’s where most pyenv functionalities come from. I agree the rest of the pyenv (shims, shell magic etc.) tends to make a mess.
- rbanffy 4y agoThis, a million times. Extra tools means extra magic and magic is always bad.
- nerdponx 4y agoYou can use Pyenv entirely as a build/version manager if you want. Just don't use `pyenv activate` and point your tools to the absolute path of the Python interpreter that you want to use. In recent versions, the shims are installed to a separate directory, which you can omit from your PATH if you never want to use them.
- vanous 4y agoI like pyenv and install it on various servers, sometimes for the latest version of python 3.xx, sometimes for 2.7, without depending on the version the OS provides. To run it from cronjob and other places where environment is harder to define, the following can be used: 0 2 * * * /home/user/.pyenv/shims/python /home/user/bin/script.py