-
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
Option to force Poetry to create a virtual environment, even if a virtual env is active #4055
Comments
What would really be nice is a way to have an active underlying conda environment upon which poetry could create a virtual environment for installing dependencies while maintaining access to the underlying conda environment. A use-case is a group of students working on a shared computer (like an HPC cluster): I would like to create a fairly full featured shared conda environment (called The behaviour I would like (if possible) is for poetry to be able to install or upgrade any missing dependencies as needed, akin to how I know that I could do this by manipulating paths Thus, daily work looks like: conda activate work # Activate read-only conda environment
poetry shell # Creates a new virtual environment with access to `work`
python ... # Works for anything using just `work`
poetry add ... # If something is not in `work` (currently fails because of permissions)
poetry install # Installed into the virtual environment like pip install --user This allows students to get to work as quickly as possible, allowing us to provide most When preparing for release, one would add the minimal set of missing dependencies poetry env use 3.8
poetry shell # Clean environment without `work`
poetry add ... # Clean up all dependences for release.
... Now students need to work harder to properly define a clean minimal set of dependencies, Any suggestions for alternatives would be appreciated. Edit: It looks like the a project-specific |
One other feature that would be really nice is if conda activate my_conda_env
poetry env use python # Tell poetry to use the my_conda_env
poetry shell # This works. After doing this, it would be very nice if this or something similar could store information about the choice of environment in |
I don't use conda, but I second this feature request anyways. In trying to debug a failing CI pipeline, it helps a lot if the venv is exclusive to the current build. On traditional Jenkins agents, that can only be assured by creating a venv in the current workspace, no matter which environments exist. Currently, I have no way (?) to force poetry to not use an existing environment (which I can't delete), which may or may not be the root of my problem. This is of particular concern since running, say, end-to-end tests with PS: Not sure whether |
I am also missing this feature, as I am migrating some code from |
I described a workaround in this answer: However, it would be nice to have the possibility to define more pyproject.toml and create other environments with the framework. |
This makes sense. I've had this happen to me again today
IMO, In this case this would still be valid poetry usage, even if poetry wasn't necessarily installing the packages. |
Any more updates/traction on this? My usecase is upgrading some projects from py3.7 to py3.10 and it's really annoying having two conda envs for each project. It will greatly help forcing poetry to create a local env every time. |
Deleting the existing virtualenv directory did help me. $ poetry run python -m site
sys.path = [
...
'/Users/user-name/Library/Caches/pypoetry/virtualenvs/some-project/lib/python3.10/site-packages',
] After this I just simply ran poetry install and a new virtualenv env was automatically created. I also added these lines to the project's
|
+1 for the |
I hava found PDM, which meets my requirements. |
Wow, PDM is like |
I faced this problem as well and created a Poetry plugin that fixes this issue for the Conda use case. Although not ideal, this solution seems to work well when I tested it. |
Referencing an earlier comment from @Secrus: #7878 (comment).
This current behavior seems very much in contradiction to the linked guidance. If installing poetry in the same environment is to be discouraged, then attempts to initialize/install a project co-located with poetry should at the very least raise a warning that this is problematic. |
Then run Poetry definitely shouldn't be trying to use an existing conda environment that doesn't contain python, and I never want it to, so a systemwide setting that lets's me turn off poetry trying to do this would be perfect. This is a national lab, so we don't generally have free reign to install system-level packages, and using up-to-date things are usually a mixture of environment modules or conda environments. |
The behavior that poetry always prefers an acitvated venv is harmful and leads to stupid workarounds and nonsensical behavior. Normally also normal users have virtual envs on their machine which act as a basis for lets say We can only force creating a venv by doing:
which will then read the The above workaround however leads to issues with other commands because its not synchronized: poetry env info --path # might be used in CI scripts to activate the correct venv ... etc etc will in the same shell not give you the just created , nameley I am not even get the option Please fix this in making this easy and comprehensible. |
Encountered this issue as well - would be great to have either:
Without it, you can accidentally install things in a virtual environment you didn't expect to. |
I just tried using poetry for the first time following the introduction guide in the documentation. In the very first chapter you see this big, red warning: If you heed this warning, installing poetry in its own environment, and continue with the example you will run into the issue described here in the very next chapter. Using your virtual environment
This is just plain wrong at this point. Poetry will not do this if you follow the guide. It will just silently use its own environment.
I have not tried this.
I have not tried this either, but from what I can see in the related issues, this is also wrong. At this point I started googling why I couldn't find any virtualenvs created by poetry. Only after that, does the documentation mention that poetry will prefer an already active virtualenv. This might clear things up if you already have some experience or you have googled the issue. But for a first time user this guide is misleading and unusable. |
@maflAT I understand the confusion if you only read the red box. However, if you read further it does not matter which method you choose (pipx, official installer, manual): all methods make sure that poetry's own virtual environment is not activated. Nevertheless, a PR improving the docs (maybe an additional warning that poetry's own environment should not be activated) is always welcome. |
…s installed in should not be activated
I think the problem does not surface when I tried to update the warning sign in #8833 |
…s installed in should not be activated
With the arrival of Pixi, this has become more of a problem. Pixi does not have a base environment by default. If you create a base environment in which to install Python and Poetry, you need to know to call it "base" or else Poetry will not work. I would recommend this as the solution: Option called |
I just came across this issue upon installing a development environment based only on I set the setting:
immediately after installing Poetry, so that no matter what, the Poetry usage would be confined to this new repository using this setup. However, after running To me, this is a bug and not a feature request. Poetry didn't do anything it was supposed to do as specified in its configuration, and it instead did the very thing I did not want it to do with no confirmation prior to doing so. |
over 3 years later and still no updates??
Okay, so I create a dedicated virtual environment for poetry. Now, as per the guidance above, I try to tell poetry to use a different virtual env for project installation by running What does poetry do? It completely ignores it's own recommendations and instead installs the project in the same virtual environment that poetry itself is installed in. Is anyone else utterly confused by this behavior?? 🤦🏻♂️ |
Did you pay attention to the last sentence of the recommendation?
|
@radoering Who and what comment are you responding to? Also, as a general comment on this issue, I see that it's still labeled as a feature request and that there's some mention above about improving the documentation, but as I described above, this is a bug. For those coming to this issue, a workaround does exist to force Poetry to ignore any
|
I responded to the comment directly above my comment but it might also apply to your comment. Respecting/using activated virtual environments is a feature even though it might look like a bug for some users. A perfectly valid workflow is to create a virtual environment without the help of Poetry, activate it and thereby let Poetry use it.
Considering the title and the description of this issue I think this issue is a feature request. |
@radoering I think that part of the issue some users have is that the instructions say - install poetry in a dedicated virtual environment, but don't activate that environment. That is not how many people expect virtual environments to work (at least with conda – Pipx makes things better, but condax still has some issues). While one can manipulate paths to use the tools installed in an isolated environment, this can cause issues if not done carefully (which is the point of activating functional conda environments). While using an active virtual environment is indeed a feature, it is strange and confusing that there is no robust way to turn off this feature when it causes issues. Perhaps poetry could refuse to update the environment in which it is installed, and emit a helpful warning at that point of use with appropriate instructions? Maybe something like:
|
With the following settings, it's expected that Poetry goes and uses another virtual environment without warning?
It is very difficult, if not impossible, for Poetry users to be able to read the official documentation, configure settings, and then be aware of what Poetry will do. So I definitely agree that there is an opportunity to improve the consistency of the documentation, which is still a "bug" in my view, but I am still confused by the behavior of some of these settings. I'd be happy to potentially take a look at doing that improvement. But it is still unclear to me what the new feature being described here would be because it seems like the feature already exists in the above settings. I do see the following note in the documentation for
In that specific note's sense, I suppose there is a feature request here, but it is confusing as to why that is the default behavior and why the above settings do not override it. So I think the bug here is that the settings and documentation and interpretation of the settings by Poetry is not consistent. So the improvement here could be:
|
The current approach of Poetry treating only the Conda base environment differently poetry/src/poetry/utils/env/env_manager.py Lines 290 to 293 in 5bab98c
likely contributes to user confusion, even though there may be valid reasons for this behavior. To enhance clarity and user control, I think it would be preferable to allow users to explicitly choose whether they want to reuse Conda environments or not. This opt-in/out approach would eliminate the need for Poetry to make assumptions about what users want, thereby providing a more transparent and predictable experience for users. |
Might make sense but you have to consider that asking for user interaction is undesirable in CI.
I understand that the interaction/meaning of the settings can be confusing. Currently, I do not know how to improve the situation. I can only try to explain the intention although I do not know if it will help or add any new information compared to the documentation:
|
pardon my ignorance, but how do you run poetry installed in a virtual environment without activating it? If this is the intended usage, then I would expect most people to be very confused by it. |
You can call the console script created in the Tools like pipx take advantage of this and only expose the packages console_scripts (so you don’t get the python of the last environment on PATH. I think there are several other tools that work similarly. I don’t think this is the primary problem being raised in this issue, however. |
Using the same env for conda and poetry destroy my poetry installation in the conda+poetry env. I have a conda env for COTS like gitleaks, trufflehog or openldap client and poetry. I installed my conda env then poetry install the dependencies in the conda env too. I wanted add a new poetry dependencies with I'm using poetry-conda plugin now to make it works. But I thing this feature should be in default installation. |
For my use case would also be very helpful in having the ability to make poetry disregard Conda or any other kind of virtual environment. This works well but I use something like Conda to install other project level dependencies that aren't necessary python ones, like for instance some dynamic libraries, ODBC drivers, and so on. Conda fits well for this as it allows to have all project dependencies in the same 'context' while maintaining each modules dependencies private to itself. On my In summary, considering the diverse use cases, it is advisable to allow users to determine when to create a new environment or reuse an existing one. For the time being, I am utilizing the poetry-conda Poetry plugin to address my requirements. However, it would be beneficial to have this plugin natively supported. |
I am using Poetry from within a
conda
environment; with Poetry being installed by conda (poetry
is present in theenvironment.yaml
file). This might not be ideal but for a specific setup this seems to work well.When running
poetry install
, avenv
is not created because Poetry detect that a virtual environment (the Conda one) is already active.I believe that we should be able to force Poetry to create a virtual environment anyway: the Conda environment is more "physical" than virtual in this case, as it replaces the system Python.
The same issue occurs when doing
poetry shell
from within the Conda environment, after creating the virtual environment with the following workaround: I deactivate the Conda environment, use the full path to Poetry (in thebin
directory of the Conda environment) and dopoetry install
.The text was updated successfully, but these errors were encountered: