You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This may be a solved problem but if it is so I haven't been able to find information in the docs or online.
My pyenv global configuration is to have two Python versions (currently 3.6.3 and 2.7.14) set at the same time. For Python packages with executables, this is sometimes ok: for example, iPython can be called with ipython2 or ipython3 without having to change the environment. But for other packages (e.g., pytest) you can only run the executable for the "front" version (in my case Python 3). Sometimes I want to by able to run pytest from the Python 2 version. Right now I have to do
pyenv shell 2.7.14
pytest
...
pyenv shell -
It would be better if one could do something line pyenv exec 2.7.14 pytest or similar. A better, but maybe more complicated options would be to create shims or symlinks to all installed executables so that they have 2 and 3 versions so that you can call pytest2 and pytest3 even if the package itself does not provide that.
Reacted by Niko Eckerskorn, Russell Davis, Germano Gabbianelli, Huon Wilson, leota, Nate McMaster, Pavel Kalashnikov, Bogdan Barna, Robert DiMartino and Alexey Trofimov
What about your distro? Arch Linux provides python2-pytest and python-pytest as packages.
If you need pytest specifically for some project, I would rather use a virtualenv for that (auto-activated).
btw: you can use a function to spawn a subshell (where you can <ctrl-d> out of).
(Useful to have $SHLVL then displayed in the prompt or some other indicator.)
I'm on Mac OS 10.13 using fish, and all my packages are pip-installed or compiled from source. Because of the way my projects and packages are organised, virtualenvs are really not that useful for me.
I'll give a try to the function snippet. It could be the solution to this problem.
Virtual environments - if pytest is installed in a virtual environment which is activated then the one it provides will be selected by the shell. This is good practice anyway to ensure your projects are using a specific supported version.
Use a tool like tox to run pytest against both versions - this also has the benefit of being share-able if others are working on your project, and reproducible even for yourself when you come back to a project after some time.
Invoking the package directly with python2.7 -m <name> - many packages (including pytest) support this out of the box and it makes it completely unambiguous which Python is being used. If a package does not support this I would consider it something to suggest in their issue tracker. This can be combined with 1 if you have .v2 and .v3 in your project folder, they can be invoked like .venv2/bin/python -m pytest and .venv3/bin/python -m pytest.
PYENV_VERSION=2.7.14 pyenv exec pytest can be entered directly in the shell or aliased to something like pytest2.
Would it be enough to promote these in the README/wiki as possible workflows?
Reacted by Germano Gabbianelli, James Szalay, Eugene Pakhomov, zrsmithson, Gary Oberbrunner and Brandon Rhodes
Not sure about items 3 and 4. At least, they didn't work in my case of Python 2.7.16 and docker-push-ssh:
$ python2.7 -m docker_push_ssh
/home/p-himik/.pyenv/shims/python2.7: No module named docker_push_ssh
$ PYENV_VERION=2.7.16 pyenv exec docker-push-ssh
pyenv: docker-push-ssh: command not found
The `docker-push-ssh' command exists in these Python versions: 2.7.16
Let me add my two cents on this. It would also be a huge help in keeping your versions up to date.
You could loop over pyenv versions and do a pyenv exec --env output_from_list python -m pip install --upgrade pip. That one seems to always be out of date when switching to a different env. This would execute twice for named envs but that wouldn't hurt much.
Even nicer would be a pyenv exec --all python -m pip install --upgrade pip which wouldn't do the duplicates.
Reacted by Richard Miles, DeepUpSonnemann, Quentin Lhoest, Iaroslav Russkykh, Florian Brucker, Andreas Motl, Gary Oberbrunner, Eunjeong Park, Andy Pan, Max Marrone and 3 more
Activity
blueyed commentedon Nov 3, 2017
What about your distro? Arch Linux provides python2-pytest and python-pytest as packages.
If you need pytest specifically for some project, I would rather use a virtualenv for that (auto-activated).
Are you using Zsh by chance?
blueyed commentedon Nov 3, 2017
btw: you can use a function to spawn a subshell (where you can
<ctrl-d>out of).(Useful to have
$SHLVLthen displayed in the prompt or some other indicator.)But it would not have tab-completion (which is not that hard to do though probably).
albireox commentedon Nov 3, 2017
I'm on Mac OS 10.13 using fish, and all my packages are pip-installed or compiled from source. Because of the way my projects and packages are organised, virtualenvs are really not that useful for me.
I'll give a try to the function snippet. It could be the solution to this problem.
dmgening commentedon Nov 16, 2017
I would appreciate such function too. In case you need a quick shell with given version or environment, it would be a lifesaver.
dmgening commentedon Nov 16, 2017
Just found out we have exec command https://github.com/pyenv/pyenv/blob/master/libexec/pyenv-exec
Would it break anything if we add optional version argument?
native-api commentedon Dec 15, 2017
If you have the version in
global/local, you can invoke it aspythonX.Y. Also see https://stackoverflow.com/questions/40718770/pytest-running-with-another-version-of-pythonchrahunt commentedon Feb 17, 2019
There are many options for accomplishing this:
python2.7 -m <name>- many packages (including pytest) support this out of the box and it makes it completely unambiguous which Python is being used. If a package does not support this I would consider it something to suggest in their issue tracker. This can be combined with 1 if you have.v2and.v3in your project folder, they can be invoked like.venv2/bin/python -m pytestand.venv3/bin/python -m pytest.PYENV_VERSION=2.7.14 pyenv exec pytestcan be entered directly in the shell or aliased to something likepytest2.Would it be enough to promote these in the README/wiki as possible workflows?
p-himik commentedon Jun 7, 2019
Not sure about items 3 and 4. At least, they didn't work in my case of Python 2.7.16 and
docker-push-ssh:But overall it works:
$ pyenv shell 2.7.16 $ docker-push-ssh usage: docker-push-ssh [-h] [-i SSH_IDENTITY_FILE] [-p SSH_PORT] ssh_host docker_image [docker_image ...] docker-push-ssh: error: too few argumentsCmdQ commentedon Nov 27, 2020
Let me add my two cents on this. It would also be a huge help in keeping your versions up to date.
You could loop over
pyenv versionsand do apyenv exec --env output_from_list python -m pip install --upgrade pip. That one seems to always be out of date when switching to a different env. This would execute twice for named envs but that wouldn't hurt much.Even nicer would be a
pyenv exec --all python -m pip install --upgrade pipwhich wouldn't do the duplicates.native-api commentedon May 19, 2021
PYENV_VERSION=<version> <command>is the current way.I've recently suggested
pyenv doas a more conventient alternative.