Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set the full name of the virtualenv created #1226

Closed
halloleo opened this issue Dec 21, 2017 · 14 comments · Fixed by #5203
Closed

How to set the full name of the virtualenv created #1226

halloleo opened this issue Dec 21, 2017 · 14 comments · Fixed by #5203
Labels
Type: Enhancement 💡 This is a feature or enhancement request.

Comments

@halloleo
Copy link

When using inside my project directory PROJECTDIR

pipenv install

pipenv creates a virtualenv named "PROJECTDIR-<someid>". How can I specify this full name myself, say to call the virtualenv just "PROJECTDIR"?

And how can I tell pipenv to use an existing virtualenv for this directory and just add the new packages from the requirements.txt?

@uranusjr
Copy link
Member

This has been discussed multiple times in the past. You can search the issue tracker for similar entries. To sum things, it is not possible (at this time; can’t say for the future) to specify the name of the virtualenv, since Pipenv relies on this information to associate it to your project. The alternative is to set environment variable PIPENV_VENV_IN_PROJECT to tell Pipenv to look for the a directory named .venv inside your project (the same level as Pipfile).

To import dependencies from requirements.txt, use pipenv install -r. This converts the requirements file to a Pipfile, and use it to create the environment. There is not way to specify an existing virtualenv (unless you name it exactly the same as Pipenv would).

Most of the above is mentioned in detail in the documentation. Please check out relevant sections for more information.

https://docs.pipenv.org

@kmpf
Copy link

kmpf commented Dec 21, 2017

I know that I can set PIPENV_VENV_IN_PROJECT environment variable, but I would like to set it in a project specific way and store that information into my git repository to share it. Would it be possible to add a section, let's say, to setup.cfg to store that information?

@techalchemy
Copy link
Member

@kmpf I believe the goal of the project is really to remain as simple and straightforward as possible and to avoid introducing additional setup complexity or configuration overhead. By sticking firmly to deterministic hashing of project paths and providing the alternative of .venv directories, there is never any ambiguity about where pipenv-managed environments will be stored.

I understand what you want and I get why you want it, I'll go even further and say that I don't even think it's unreasonable. However I do think it is out of scope -- it is something that has been requested a few times though, and this is an open source project. I would strongly encourage anyone who needs additional layers of functionality to feel free to build and publish tools that provide that

Thanks for the report, and thanks for using our tools :)

@TauPan
Copy link

TauPan commented Apr 26, 2018

My workaround:

Current versions of pipenv respect an existing virtualenv, so if you want to control the name you could (with virtualenvwrapper)

mkvirtualenv [-a ~/projects/my-project-dir ] [-p $my_python_executable] my-project
pipenv install --dev

pipenv install will notify that it will use the existing virtualenv. In order to keep using it, you'll have to remember to workon my-project before using pipenv.

@janjur
Copy link

janjur commented Apr 9, 2019

My use-case: As a spaceship-prompt user I was annoyed that PROJECTDIR-<someid> atrocity was displayed at the end of my prompt:
image
My workaround: modify the way spaceship-prompt displays virtualenv name.

Either
add venv=${venv%-*} before spaceship::section in $SPACESHIP_ROOT/sections/venv.zsh
or
run sed -i '39i\ venv=${venv%-*}' $SPACESHIP_ROOT/sections/venv.zsh.

Works with spaceship-prompt 3.11. Replace sed to gsed (after brew installing it) if MacOS user.
Result:
image

@cdvv7788
Copy link

Is there a way to retrieve the name of the venv associated with a project? I mean, when I am not directly in the project path, can I figure out the name of the venv associated with it?

@qhelix7
Copy link

qhelix7 commented Mar 13, 2020

Is there a way to retrieve the name of the venv associated with a project? I mean, when I am not directly in the project path, can I figure out the name of the venv associated with it?

The venv name is deterministic and based on the path to the Pipfile. It is just project-xxxxxxxx, unless your project name needs to be sanitized of dangerous symbols and/or truncated to 42 characters. The code for generating the name is in pipenv.project.Project._get_virtualenv_hash().

This one-liner will get you the hash:

python -c 'import sys; import base64; import hashlib; print(base64.urlsafe_b64encode(hashlib.sha256(sys.argv[-1].encode()).digest()[:6]).decode()[:8])' /path/to/project/Pipfile

@ianyoung
Copy link

One of my main issues with not being able to choose a name for the virtual environment is that it doesn't take into account varying folder structures. I usually follow a common convention and like to nest my source code inside a /src directory of my main project folder. In the /Users/NAME/.local/share/virtualenvs directory every virtual env looks like:

src-Q4dXJo6L
src-ha_K95Ys
...

This makes it a little harder to distinguish between them.

This is also problematic when you have a frontend and backend in your repo, each with separate Pipenv files for separate dependencies. Again, I'd ideally like to name the project PROJECT-frontend and PROJECT-backend. With the hash suffix on both of those.

It would be nice to have a little control on the virtual environment naming.

@melvyn-apryl
Copy link

In the /Users/NAME/.local/share/virtualenvs directory every virtual env looks like

So you bought 10 red Ferrari's and now, looking at your garage, you don't know any more which one you used yesterday. There are a few cases where this is inconvenient (like, which one did I put the dead guy in), but most of the time, you just want to get from A to B in style.

Given your approach of looking at the virtualenv home, a quick look in src-*/lib/*/site-packages/ will reveal which is which, but the easiest approach is simply to go your project directory and use pipenv --venv, so you know the one used by that project. Heck, if you don't like the double shell spawning of pipenv shell, you can even do source $(pipenv --venv)/bin/activate.

All I see here, is cosmetics and minor inconvenience for things not done often. Can we move on?

@darelf
Copy link

darelf commented Mar 3, 2022

This is what I use. I just fork pipenv, add 3 lines, install it local. ( pip install --user . )

main...darelf:main

export PIPENV_CUSTOM_VENV_NAME=mycustomname
pipenv install
pipenv shell
etc. etc.

@halloleo
Copy link
Author

halloleo commented Mar 3, 2022

Thanks @darelf! Good idea.

@matteius
Copy link
Member

matteius commented Mar 4, 2022

@darelf Care to open a PR patch against the main library since it seems that more than a couple users would like this feature?

With all due respect to @techalchemy 's 2017 comment about it that closed the issue, I think it could be supported and may even reduce the number of support tickets if people can have predictable .virtualenv names configured as needed, non-default.

@oz123
Copy link
Contributor

oz123 commented Aug 4, 2022

This is finally fixed ❤️. Thank you for your feedback and involvement.

@matteius
Copy link
Member

matteius commented Aug 5, 2022

New pipenv release 2022.8.5 has been pushed to pypi which includes this feature request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement 💡 This is a feature or enhancement request.
Projects
None yet