question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Let user choose location of .venv

See original GitHub issue

It’s irritating that you cannot specify custom virtual environment directory location. It makes pipenv unsuitable for production deployments. PIPENV_VENV_IN_PROJECT is suitable only for development environments.

Consider this scenario using docker-compose.yaml to set up a Docker container:

...

  install:
    image: python:3.8
    environment:
      PIPENV_VENV_IN_PROJECT: 1
    volumes:
      - /srv/deploy/app:/app:ro # Note that this is bind mounted read-only
      - /var/cache/libgen-tools-venv:/app/.venv
    # Pipenv writes by default so we pass --keep-outdated
    # because Pipfile.lock is on read-only filesystem.
    command: pipenv sync --keep-outdated

  periodic-batch-job-1:
    image: python:3.8
    environment:
      PIPENV_VENV_IN_PROJECT: 1
    volumes:
      - /srv/deploy/app:/app:ro # Note that this is bind mounted read-only
      - /var/cache/libgen-tools-venv:/app/.venv:ro
    command: pipenv run python3 /app/dostuff.py

  ...

Here the project is deployed out of Git/CI/CD into /srv/deploy/app and is mounted read-only to containers at /app, because more containers share the same code and they have no business overwriting it. But this means that /app/.venv cannot be created in containers.

This is very specific example I’m now just dealing with, but having virtual environment in /var/cache or any other volatile directory seems to be perfectly legal usage.

I’ve experimented with exporting VIRTUAL_ENV prior to running pipenv install but it is ignored.

This issue was previously closed here https://github.com/pypa/pipenv/issues/746.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
OrangeDogcommented, Apr 30, 2021

@ZelphirKaltstahl but pipenv should create and activate the virtualenv as necessary, otherwise you way as well just use pip freeze.

2reactions
techalchemycommented, Apr 27, 2020

Hey there, sorry you’re running into this frustration – I’ve struggled with this myself and I definitely hear you. You have a couple of options already which address this use case broadly speaking – not all of these will apply to your specific use case but I will include them in case they help anyone else:

  1. Setting VIRTUAL_ENV should work ok, as should sourceing a virtual env before invoking any calls (though you’d have to do so ahead of each call);
  2. You can include a .venv file which contains text with a path to a virtual env, e.g. /path/to/some/venv_base, which pipenv will then treat as the actual environment;
  3. You can forego the virtualenv entirely and install using --system – this would install directly into the container’s environment obviously, so YMMV;
  4. You can set WORKON_HOME to /var/cache/app_venvs and make sure to create the directory and give whomever will be writing the appropriate access; pipenv will then automatically create the virtualenv there based on a deterministic hash of the application path

As for the comment in there about pipenv writing everything it sees, yeah, that issue is also super annoying. I expect to tweak that API a bit sometime soon to try and make that a bit less painful.

Let me know if any of that can meet your use case – thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to specify location of virtualenv? - Stack Overflow
With virtualenvwrapper you can specify where to store your envs using the WORKON_HOME environment variable. If you want just a specific ...
Read more >
Python Virtual Environments: A Primer
In this tutorial, you'll learn how to work with Python's venv module to create and manage separate virtual environments for your Python ...
Read more >
User Guide — virtualenv 16.7.11 documentation
Virtualenv has one basic command: $ virtualenv ENV. Where ENV is a directory in which to place the new virtual environment.
Read more >
Python virtualenv and venv dos and don'ts - InfoWorld
A word of advice on virtual environment creation: Don't name the directory of your virtual environment venv —or, for that matter, the name...
Read more >
Python Virtual Environments in Five Minutes | Chris Warrick
Have one global place for them, like ~/virtualenvs . Store them in each project's directory, like ~/git/foobar/.venv . The first option can ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found