admin管理员组文章数量:1200385
On Windows, I have a poetry project which references Python 3.12, and includes dependencies to package A and package B. Poetry was added to my system using pipx in the context of Python 3.12.
I also have Python 3.8 on my machine with package A installed in the global environment via pip install. This is because I have a python add-in in another piece of software which requires Python 3.8. This add-in also requires that I have a system environment variable PYTHONPATH which references the location of my 3.8 packages (usr\AppData\Local\Programs\Python\Python38\Lib\site-packages) so that it can find where to search for packages.
Anyhow, when I try to run a script in my poetry project, I get an import error on "import package A" because it is evidently trying to import first from the Python38 global library rather than the poetry-managed VE, and since package A happened to be installed in the Python38 global library, it uses that version which is not compatible. It still manages to find package B without any trouble, even though package B is only installed in the poetry VE.
If I remove the PYTHONPATH system env variable, everything runs as expected - it uses only the packages it finds in the poetry VE.
Why does Poetry look first in the location of PYTHONPATH rather than its VE? How can I prevent this behavior? Or is there something else I am missing?
I searched around and have not found this specific problem mentioned anywhere.
Step-by-step to reproduce this:
- Install Python 3.12, and install poetry using pipx per standard poetry installation approach.
- Install Python 3.8, and add a system environment variable:
PYTHONPATH = C:\Users\username\AppData\Local\Programs\Python\Python38\Lib\site-packages
- In context of Python 3.8 global environment, pip install numpy.
- Create a poetry project in context of python 3.12
poetry new testProject
. - In new project (
cd testProject
), add numpy and keyboard:poetry add numpy keyboard
. - To testProject main folder, add file 'test.py' with contents:
import keyboard
import numpy
- Do
poetry run test.py
; keyboard imports fine, but on import numpy you receive (partial) output below:
PS D:\path\to\poetry\project\testProject> poetry run .\test.py
Traceback (most recent call last):
File "C:\Users\######\AppData\Local\Programs\Python\Python38\Lib\site-packages\numpy\core\__init__.py", line 23, in <module>
from . import multiarray
File "C:\Users\######\AppData\Local\Programs\Python\Python38\Lib\site-packages\numpy\core\multiarray.py", line 10, in <module>
from . import overrides
File "C:\Users\######\AppData\Local\Programs\Python\Python38\Lib\site-packages\numpy\core\overrides.py", line 6, in <module>
from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
- As can be seen, poetry attempts to run numpy from the global Python 3.8 site-packages folder.
- Remove system environment variable PYTHONPATH created above, and in new power shell window, run
poetry run test.py
again, with no error.
On Windows, I have a poetry project which references Python 3.12, and includes dependencies to package A and package B. Poetry was added to my system using pipx in the context of Python 3.12.
I also have Python 3.8 on my machine with package A installed in the global environment via pip install. This is because I have a python add-in in another piece of software which requires Python 3.8. This add-in also requires that I have a system environment variable PYTHONPATH which references the location of my 3.8 packages (usr\AppData\Local\Programs\Python\Python38\Lib\site-packages) so that it can find where to search for packages.
Anyhow, when I try to run a script in my poetry project, I get an import error on "import package A" because it is evidently trying to import first from the Python38 global library rather than the poetry-managed VE, and since package A happened to be installed in the Python38 global library, it uses that version which is not compatible. It still manages to find package B without any trouble, even though package B is only installed in the poetry VE.
If I remove the PYTHONPATH system env variable, everything runs as expected - it uses only the packages it finds in the poetry VE.
Why does Poetry look first in the location of PYTHONPATH rather than its VE? How can I prevent this behavior? Or is there something else I am missing?
I searched around and have not found this specific problem mentioned anywhere.
Step-by-step to reproduce this:
- Install Python 3.12, and install poetry using pipx per standard poetry installation approach.
- Install Python 3.8, and add a system environment variable:
PYTHONPATH = C:\Users\username\AppData\Local\Programs\Python\Python38\Lib\site-packages
- In context of Python 3.8 global environment, pip install numpy.
- Create a poetry project in context of python 3.12
poetry new testProject
. - In new project (
cd testProject
), add numpy and keyboard:poetry add numpy keyboard
. - To testProject main folder, add file 'test.py' with contents:
import keyboard
import numpy
- Do
poetry run test.py
; keyboard imports fine, but on import numpy you receive (partial) output below:
PS D:\path\to\poetry\project\testProject> poetry run .\test.py
Traceback (most recent call last):
File "C:\Users\######\AppData\Local\Programs\Python\Python38\Lib\site-packages\numpy\core\__init__.py", line 23, in <module>
from . import multiarray
File "C:\Users\######\AppData\Local\Programs\Python\Python38\Lib\site-packages\numpy\core\multiarray.py", line 10, in <module>
from . import overrides
File "C:\Users\######\AppData\Local\Programs\Python\Python38\Lib\site-packages\numpy\core\overrides.py", line 6, in <module>
from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'
- As can be seen, poetry attempts to run numpy from the global Python 3.8 site-packages folder.
- Remove system environment variable PYTHONPATH created above, and in new power shell window, run
poetry run test.py
again, with no error.
1 Answer
Reset to default 0This is not a poetry question, it is a python question.
You have set an environment variable that modifies the behaviour of the python interpreter. Then you have run a python program: and it has respected the behaviour that you have asked for.
Per https://docs.python.org/3/library/sys_path_init.html
PYTHONPATH will affect all installed Python versions/environments. Be wary of setting this in your shell profile or global environment variables
If that was not what you wanted - then unset the environment variable.
本文标签:
版权声明:本文标题:python - Poetry looks for packages in global PYTHONPATH first instead of in poetry-managed virtual environment - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738605533a2102323.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论