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.

Document that `poetry install` modifies PYTHONPATH

See original GitHub issue
  • I have searched the issues of this repo and believe that this is not a duplicate.

Issue

Thank you very much for taking some of your valuable time to read this. I hope it’ll be worth it!

What happens?

poetry install with an existing conda environment and src layout silently and permanently adds repo/src to the environment’s PYTHONPATH. This behaviour is not documented (or, I didn’t find it).

$ conda create -n withsrc_py37 python=3.7
$ conda activate withsrc_py37
$ poetry new --src my-src-package
$ cd my-src-package
$ poetry run python -c "import sys, pprint; pprint.pprint(sys.path)"
['',
 'C:\\Users\\THW\\AppData\\Local\\Continuum\\anaconda3\\envs\\withsrc_py37\\python37.zip',
 'C:\\Users\\THW\\AppData\\Local\\Continuum\\anaconda3\\envs\\withsrc_py37\\DLLs',
 'C:\\Users\\THW\\AppData\\Local\\Continuum\\anaconda3\\envs\\withsrc_py37\\lib',
 'C:\\Users\\THW\\AppData\\Local\\Continuum\\anaconda3\\envs\\withsrc_py37',
 'C:\\Users\\THW\\AppData\\Local\\Continuum\\anaconda3\\envs\\withsrc_py37\\lib\\site-packages']
$ poetry install
$ poetry run python -c "import sys, pprint; pprint.pprint(sys.path)"
['',
 'C:\\Users\\THW\\AppData\\Local\\Continuum\\anaconda3\\envs\\withsrc_py37\\python37.zip',
 'C:\\Users\\THW\\AppData\\Local\\Continuum\\anaconda3\\envs\\withsrc_py37\\DLLs',
 'C:\\Users\\THW\\AppData\\Local\\Continuum\\anaconda3\\envs\\withsrc_py37\\lib',
 'C:\\Users\\THW\\AppData\\Local\\Continuum\\anaconda3\\envs\\withsrc_py37',
 'C:\\Users\\THW\\AppData\\Local\\Continuum\\anaconda3\\envs\\withsrc_py37\\lib\\site-packages',
 'c:\\users\\thw\\repos\\my-src-package\\src']  # new!

This change to sys.path persists after conda deactivate and conda activate withsrc_py37.

Why am I surprised?

The idea (for me) with using the src layout is to make it really difficult to run tests against my source code. I want the tests to run against the built and installed package instead, as per hynek and ionelmc.

Making matters more murky, tox’s test environments appear to inherit this PYTHONPATH. This puts the supposedly independent tox runs at risk of using the source code.

Why not a bug?

I don’t know if this is a bug, but I expect not: It seems that there’s no consensus in the community whether the src layout is a good or bad idea, so this behaviour is quite possibly expected for some use cases. And besides, now that poetry is in stable, I guess documenting the current behaviour should come before changing it 😁

Additional details

Output of poetry debug info:

Poetry
Version: 1.0.0
Python:  3.7.5

Virtualenv
Python:         3.7.5
Implementation: CPython
Path:           C:\Users\THW\AppData\Local\Continuum\anaconda3\envs\withsrc_py37
Valid:          True

System
Platform: win32
OS:       nt
Python:   C:\Users\THW\AppData\Local\Continuum\anaconda3\envs\withsrc_py37

What seems to be a highly related PR, “Add src to sys.path if module is in src”: https://github.com/python-poetry/poetry/pull/123

A PR that added documentation for the --src flag: https://github.com/python-poetry/poetry/pull/193

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
finswimmercommented, Mar 20, 2021

Hello everyone,

as written in the docs a poetry install installs the current project in editable mode. This is necessary to be able to run your code immediately after changing it, without going through the cycle “edit file” -> “build package” -> “install package” -> “run command”.

To achieve this poetry uses the path configuration file method described here https://docs.python.org/3/library/site.html

You can avoid installing the package itself by running poetry install --no-root instead.

If you want to make sure your tests are running against the builded packages, your tests should run in an isolated environment as well using e.g. tox. With pytest 6.0 --import-mode importlib was introduced to make sure to run against the builded package. I wrote about it at stackoverflow as well.

fin swimmer

1reaction
dedalecommented, Mar 19, 2021

Thanks a lot @thorbjornwolf for creating this issue, I’ve been scratching my head for two hours trying to figure out why in some cases my modules were found and ModuleNotFoundError in other cases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Modifying Poetry's virtual environment PYTHONPATH
Create a package folder first which then contains all your modules. Don't forget to run poetry install once. This will install the project...
Read more >
Introduction | Documentation | Poetry - Python dependency ...
Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and...
Read more >
Configuration | Documentation | Poetry - Python dependency ...
By default, Poetry is configured to use the PyPI repository, for package installation and publishing. So, when you add dependencies to your project,...
Read more >
Basic usage | Documentation | Poetry - Python dependency ...
Project setup # ... Poetry assumes your package contains a package with the same name as tool.poetry.name located in the root of your...
Read more >
Configure a Poetry environment | PyCharm Documentation
Poetry is a tool that facilitates creating a Python virtual environment based on the project dependencies. You can declare the libraries your project ......
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