Skip to main content

Project-level Python virtual environment management tool.

Project description

PyEM manages multiple virtual environments local to projects. It provides shortcuts to create, remove, switch between, and run commands against virtual environments created against various Python interpreters.

Install

I recommend using pipx:

pipx install pyem

# If you need to support Python without the builtin venv module.
pipx inject pyem virtualenv

In Action

Add a virtual environment besides the file pyproject.toml:

# Based on a command.
pyem venv add python3.7

# Based on interpreter found by the Python launcher.
pyem venv add 3.6

# Based on an executable.
pyem venv add /usr/local/bin/pypy3

The second variant relies on the Python launcher to locate an interpreter. This tool should be installed by default if you use the officlal installer on Windows (and do not explicitly choose not to install it). For other platforms, Python launcher for UNIX by Brett Cannon can be used as an alternative.

List managed virtual environments:

$ pyem venv list
  Quintuplet
=============================================
  cpython-3.6-darwin-x86_64-f14a3513
  cpython-3.7-darwin-x86_64-dbe83ac5
* pypy-3.6-darwin-x86_64-dc1298a1

Set active virtual environment:

$ pyem venv set 3.7
Switched to cpython-3.7-darwin-x86_64-dbe83ac5

$ pyem venv set 3.6
Error: name '3.6' is ambiguous; choose from:
  cpython-3.6-darwin-x86_64-f14a3513
  pypy-3.6-darwin-x86_64-dc1298a1

$ pyem venv set cpython-3.6
Switched to cpython-3.6-darwin-x86_64-f14a3513

Run a command inside a virtual environment:

$ pyem poetry run python -c "import sys; print(sys.executable)"
/tmp/exampleproject/.venvs/bin/python

$ pyem --spec=pypy-3.6 pipenv run pypy3 -c "import sys; print(sys.executable)"
/tmp/exampleproject/.venvs/bin/pypy3

How does this work?

PyEM sets environment variables VIRTUAL_ENV and PATH, and hand off control to subprocess for the command specified. This is enough to trick a lot of Python project tools, including the Python launcher, Poetry, and Pipenv. Python interpreters with venv support (e.g. CPython 3.3 or later) should also integrate seamlessly.

Tips and Tricks

Flit on Windows

Flit has problems detecting the active virtual environment on Windows when installed into Python 3.7.2 or later. Use the following workaround (requires the Python launcher):

$ pyem flit install --python=py

Starting from Flit 2.1, you can also set the environment variable FLIT_INSTALL_PYTHON=py for the same effect. This is a good default even when you’re not using PyEM IMO; it makes more sense than installing into Flit’s environment.

This has been fixed in master (takluyver/flit#300), so Flit after 2.1.0 does not need this workaround.

Project without pyproject.toml

If your project does not use pyproject.toml, you can specify the project root explicitly:

pyem --project=./myproject add 3.8

The --project option is only required when creating a virtual environment. Subsequent commands should pick up the .venvs directory automatically, and use its location as the project root, even without the presence of pyproject.toml.

Call a virtual environment outside the project root

The --project option is also handy if you want to access a virtual environment when you’re outside of the project root. This command lists installed packages in the 3.7 virtual environment of another-project:

pyem --project=../another-project --python=3.7 pip list

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyem-1.0.1.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyem-1.0.1-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file pyem-1.0.1.tar.gz.

File metadata

  • Download URL: pyem-1.0.1.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.22.0

File hashes

Hashes for pyem-1.0.1.tar.gz
Algorithm Hash digest
SHA256 af3c2cc037b2e073c3e73e5d75c7e8bcd5c1ac9e5724924bfd51888547249b08
MD5 6e20d5eab98d2c6aca9df634d5b15663
BLAKE2b-256 be8ffb456bf827651e4e1856633f36fe4f33556ff0b66583c8790b94a2ecc125

See more details on using hashes here.

File details

Details for the file pyem-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pyem-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.22.0

File hashes

Hashes for pyem-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c7524f18ece2697c63307d1a6d0f8a897aaf4651899dd0c39ff0925587129787
MD5 73d78cf1adb730ac981c8f31e13400c1
BLAKE2b-256 0a87aa2c7e97152eee48b06bded7ad546bf1b3526675c246dc58c2c646f09843

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page