Skip to content

Run command with specific version #1028

Closed

Description

@albireox

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.

Activity

  1. blueyed commented on Nov 3, 2017

    @blueyed
    Contributor

    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?

  2. blueyed commented on Nov 3, 2017

    @blueyed
    Contributor

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

    pyenv-exec() { $(pyenv shell "$1" && shift && pyenv exec "$@") }

    But it would not have tab-completion (which is not that hard to do though probably).

  3. albireox commented on Nov 3, 2017

    @albireox
    Author

    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.

  4. dmgening commented on Nov 16, 2017

    @dmgening

    I would appreciate such function too. In case you need a quick shell with given version or environment, it would be a lifesaver.

  5. dmgening commented on Nov 16, 2017

    @dmgening

    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?

  6. native-api commented on Dec 15, 2017

    @native-api
    Member
  7. chrahunt commented on Feb 17, 2019

    @chrahunt
    Member

    There are many options for accomplishing this:

    1. 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.
    2. 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.
    3. 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.
    4. 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?

  8. p-himik commented on Jun 7, 2019

    @p-himik

    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

    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 arguments
  9. CmdQ commented on Nov 27, 2020

    @CmdQ

    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.

  10. native-api commented on May 19, 2021

    @native-api
    Member

    PYENV_VERSION=<version> <command> is the current way.

    I've recently suggested pyenv do as a more conventient alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions