-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Manually specifying the venv path #1579
Comments
@viniciusd Is this what you are looking for: https://poetry.eustace.io/docs/configuration/#settingsvirtualenvspath-string ? |
Not exactly. This one defines where each project's virtual environment will be set. |
pyenv solves this by |
Hey @viniciusd , could you describe in which scenario two projects needs to share the same virtuell environment? Thanks. fin swimmer |
@finswimmer Other usecases are setting the name for venv or using an already existing venv for new project. |
can this not be solved with https://poetry.eustace.io/docs/configuration/#settingsvirtualenvspath-string |
Nope. It just gives a base dir for all venvs. |
To answer this first: AFAIK there is no way to do this (at the moment).
Mhh, I wouldn't do this. It looks to me like a bad practices. New projects should start with a fresh virtual environment to ensure only dependencies needed are installed. Different projects should have different environments to avoid any conflicts when (de)installing/updating/downgrading a package for one projects. One use case for specifying the path to the venv I can imagine, is when you run out of space and one need to put the venv files to a different location. A workaround (I haven't tested) might be as follow: When fin swimmer |
To be honest, i don't think this is not about a being good/bad practice. Personally, i don't want to see |
In addition to what @ulgens mentioned, a use case would be having a shared project with shared modules and dependencies in order to guarantee the different services/projects are using the same versions of the shared dependencies. |
on deployment jobs is also useful/common to setup the environment in stages and with more flexibility, currently I have to export to requirements.txt to then use the correct env with other tools. |
The more I think about this feature request and the more you told me about the use cases, the less I'm convinced that poetry should support is. poetry is about managing python projects and not environments. poetry is also about best practices, as it always seeking for solutions that are already standardized or are respected as those. Also it limits its configuration to be able to have one way to do it and not many. This makes the projects highly compatible to another and on different platforms. Being able to define the path to the virtual environment and so makes it possible that multiple projects uses it, introduces another problem: Who is responsible that the The clean way would be, that you define a new environment on your system, where in the end all projects should play together, that have these projects as dependencies. |
Poetry can create virtual environments, activate and deactivate them, use existing ones, but not allows to define a venv path. I'm not sure this is a "best practice".
I think this is out of scope. For given usecases, it's not really important and people already have other solutions. I feel like you're missing the point. It's not about changing the default behaviour. I can set the tool to put venv in the folder and i can create a symlink from venv folder to real environment, but what if i don't want to do it for every project? Is it a bad thing if we have that possibility? There is some strong words about why Poetry shouldn't implement it but it's already here, just half baked. |
My workflow that I think could take advantage of this is using tox to run tests. Currently, when you use Being able to use a specific environment (or even the "active" environment) would remove this problem, and make running tests using tox isolated. You can't change the settings on poetry to temporarily change the venv location, because those settings are always user-global: and there's no guarantee you are the only |
FWIW: I have a workaround: you can use |
I think this would be a useful feature to specify the exact virtualenv you want to use. For example, I'm using Poetry inside of a Docker container and I'd like to specify the exact directory where the virtualenv should be created. And I don't want to use the "in-project" setting because while I'm developing with docker or docker-compose, I often like to mount in my source code into the docker container (so I can make live updates).... but I don't want to mount in the virtualenv necessarily. I'd like to exactly specify where in the container I'd like to create the virtualenv so all of my configuration and scripts can reference that location. When I set Maybe there's a way to do this already - I'm still getting used to the new 1.0 features. |
If you need a predictable ENV PYTHONPATH=/opt/example/example-7rCCnWI7-py3.7/lib/python3.7/site-packages
RUN poetry env use `which python`
RUN test "$(poetry env list | cut -f 1 -d ' ')" == "example-cHSZFygF-py3.7" The |
Inside a docker container you can also prevent poetry from creating virtual environments: this will be local to the docker container. RUN pip install poetry
WORKDIR /app
COPY poetry.lock pyproject.toml /app/
RUN poetry config virtualenvs.create false
RUN poetry install --no-dev -vvv |
I'd also like to see a possibility to specify path to virtualenv manually. It's useful in docker and possibly in other use cases too. I know that I can create the virtual env manually, activate it and then run poetry in it but it seems like unnecessary hassle considering how poetry makes my life easier in other areas. |
As a novice in package management, this is my usecase where specifying the venv path would be useful. If I can install A inside B's venv then I would not need to install B twice. https://stackoverflow.com/questions/60287564/how-to-manage-editable-shared-libraries-with-poetry |
@ulgens I don't see any documentation on a How do I use it? I tried creating a .venv file with contents I can't find any info on how to automatically activate the poetry venv using pyenv :( Edit: I avoided the issue alltogether by just configuring poetry to not create venvs |
Specifying just a specific name (without the hash) would be good enough for me, and I can't seem to find a nice way of doing that. My use case is having a venv setup for neovim that contains some packages that my neovim plugins need. I used to just set the I have similar issues in VSCode with Python paths. |
Hey @geckon , I'm trying to do this exact thing (create a virtual env manually and then use poetry inside of it), but poetry for some reason is not detecting that virtual env. An important thing, I'm trying to do it in a Dockerfile. I activate the virtual env, and then I set the VIRTUAL_ENV var with the venv directory, and poetry is still creating a virtualenv on their own. Do you know how can I do this? |
@jagretti, you should be able to do something like this...
|
Or if that doesn't work for some reason, you can try something like:
|
How would you do this using the recommended poetry installation I would still like to be able to have my poetry installation separate from my virtualenv in order to avoid installing any of poetry's dependencies without having specified them in Edit: Actually I think I figured it out. This should install poetry as a separate installation, and then cause
|
For people still wondering about use cases. I have two python projects, one is the central project and the other is a plugin for the project. I'd like have each be installed and editable in the same environment as I am often making changes to both in tandem. In the past I would simply do |
@JulianFerry be aware that poetry settings can not be applied to just the shell or location: they are user-global. |
Thanks. I am just using this for docker builds, which means this is not an issue. But as you say, perhaps there could be a better solution for development environments. |
I have the same use case as @theirix. Here is the best workaround I've found so far to get poetry to use an explicit venv:
|
My use case: I have a python script (call it "driver.py") that uses its own non-poetry virtual environment to execute commands that themselves need to be run a poetry environment. I want driver.py to execute: "poetry run meson compile -C build". It fails because poetry picks up driver.py's virtual environment. As a workaround, I would like to be able to setup a named poetry virtual environment that I can refer to with poetry run. For example:
Then my driver script, which uses asyncio, could do something like this:
|
@geckon wrote:
This is how we solve the problem right now, but the downside of that approach that we need to maintain Python version in two places:
|
Like how @ulgens very well put it back in 2019 and there is still no resolution:
It disappoints me how such an integral feature is getting a push back from the community on no grounds. |
This is exactly my use case- multistage docker builds. I want to create a |
I am working with a program that allows plugins. |
Another use case would be a Docker bind mount. When managing dependencies inside a Docker container I would want the |
I've used the following method with pipenv and it seems just as effective with poetry as well. ENV VIRTUAL_ENV=/usr/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN python3 -m venv $VIRTUAL_ENV \
&& poetry install --no-interaction --no-ansi --no-dev Inspired by this article. |
Looks like most use cases for this (particularly docker) are covered by #108 FROM base_image as build
# I think this feature is coming in 1.2 so may not work with pip install yet
RUN --mount=type=cache,target=/root/.cache \
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
RUN --mount=type=cache,target=/root/.cache \
PATH=/root/.poetry/bin:${PATH} poetry config virtualenvs.in-project true
WORKDIR /app
COPY /source /app/source
COPY pyproject.toml /app
COPY poetry.lock /app
# Creates a venv that with the stable path /app/.venv
RUN --mount=type=cache,target=/root/.cache \
PATH=/root/.poetry/bin:${PATH} poetry install --no-dev
FROM base_image
COPY --from=build /app /app
ENV VIRTUAL_ENV /app/.venv
ENV PATH /app/.venv/bin:${PATH}
|
In case it is helpful, I have a full |
(from @kellerza)
(from @johnthagen)
This is the solution, and for those who don't follow, why this issue is considered out of scope for Poetry. Poetry already has a bail-out for users who need more complicated virtual environment management than our (intentionally minimally featured) built-in solution offers -- If Poetry detects an already activated virtual environment, it will use it instead of creating its own. Use of |
There are so many other use cases than Docker that have been thoughtfully explained in the comments above. (There are probably hundreds of 👍’s on comments not related to Docker.) Clearly this feature is important to a lot of people, so it is very disappointing to see it closed. Even if there are “workarounds”, having a built-in option to help deal with “more complicated virtual environment management” means that much less manual tooling, training, debugging, etc. for every user of Poetry in that situation. |
this would be a nice feature to have and clearly people want it. would be convenient to not have to use other tools for a workaround in case anyone finds this useful [apologies if it has already been mentioned and i missed it above], here is the workaround I use for activating docker venvs:
if you only want to store the path then you could export it as an environment variable in your
note that these commands need to be run from the same dir as your main advantage of the above approach is sticking with just poetry rather than using venv directly too. arguable whether that's much of an advantage vs the hope that's useful for someone |
You should not depend on In any case, in a container, it makes much more sense to control paths and be explicit, which not only makes your image build more understandable to other contributors, but can improve your layer caching strategy. There seem to be two schools of thought: Those that think that Poetry is a composable tool that tries to make Python development as easy as possible while being interoperable, and those who want to replace the entire Python tooling ecosystem with Poetry. The thoughts of the maintainers tend towards the middle, leaning towards interoperability when there is a perceived conflict. Poetry is aiming to improve the common use case while not inventing a parallel universe where it makes decisions incompatible with other tools, or where you are forced to use Poetry exclusively and not integrate it into existing workflows. I do think our unobtrusive virtual environment management is a good example of striking this balance -- the simple case is abstracted for users, and the complex cases are easily solved by the rich tools that focus on virtual environment management. Cases in the middle, like containers, benefit from the standard tooling shipped with/maintained by the core Python project that all Python developers should be familiar with. |
I agree that it would be nicer to do this in dockerland by controlling the path, which would be easier if poetry were to allow us to specify a venv path. as you said, the your general position outlined above is sensible - poetry can't be expected to do everything. however, i do think that turning down a feature request to allow users to specify a venv name/path is a little inconsistent with the other features poetry offers. why draw the line at providing a name for the directory where poetry will create a venv? poetry already creates and names venvs, why would this qualify as obtrusive? the cli has the following option:
adding a |
poetry 1.2.2 refuses using the existing virtual environment with the suggested solution:
With So far, I have found no way to get 1.2.2 to use an existing environment created by |
@cpbotha appending to a closed ticket is a good way of not getting help. Your snippet is identical (in thrust) to what I do at work.
Please, always give outputs, error messages, or really clear descriptions of what goes wrong. |
I am very curious to hear how you did that with 1.2.2. See below for a small test, first inputs then outputs, with
BTW, I posted here for others to find, and to discuss, because this is the ticket that comes up about this specific topic of manually specifying the venv path. |
In my case, installing poetry into the @finswimmer Thank you very much for the offer! I like discord, but when I searched for a solution to the issue I was seeing, THIS issue was the main and most important hit. My reasoning is that others who search for similar solutions will also find this thread, so it makes sense to have helpful info here as well. |
I understand and appreciate it 🙏 But you came to the point where something doesn't work for you but works for @clintonroy . So finding out what's going on shouldn't be part of this (closed) issue here.That's better done on discord or a separate issue. Coming back after everything is resolved and provide a solution is fine than. |
@finswimmer If you consider how many people are requesting this feature with valid use cases and how weak the explanation about why it shouldn't be implemented is, having multiple people commenting on a "closed" issue may be an indicator of that issue shouldn't be closed. |
Discussion on this issue has gone wildly off topic -- the proposed feature of specifying the path Poetry uses for it's built-in environment management (both This issue is going off topic, so I am going to lock it for now as it's attracting support questions. In general, if you have a support question, please create a Discussion or join Discord; if you have done through troubleshooting and think you have identified a bug, please open a new issue. Overloading issues with multiple discrete variations or "me too" comments rarely does anything but add noise -- if something is a unique bug, we need to be able to track it and reason about it separately. Thanks all -- and again, feel free to open Discussions or ask on Discord about this topic. The goal here isn't to discourage discussion, but to make it clear that this is currently rejected as a feature -- you're free to talk about it elsewhere and advocate for it, or even propose a PR (though, unless it does something novel not already discussed, it's likely not going to be accepted at this time). |
Is it possible to manually (via some config) specify which venv poetry is going to use?
Due to some requirements, I would like to have two projects sharing the same virtual environment.
Best case scenario would be setting the path via a config or environment variable
The text was updated successfully, but these errors were encountered: