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.

Add specified directories to the PYTHONPATH

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

Feature Request

Poetry is a very useful tool, not just for developing Python packages, but also for managing Python dependencies when developing in general (as a replacement tool for requirements.txt for example). In developing environments, a very useful tool is to be able to append permanently to the PYTHONPATH.

For example, when working with Jupyter (standard in data science projects), the code is being executed from the folder where the Jupyter notebook is, so if it isn’t in the project’s root directory, importing code from your own code will fail. See a detailed example in my SO question.

The feature I am suggesting is equivalent to virtualenvwrapper add2virtualenv so it will be possible to permanently add to the PYTHONPATH of poetry-managed environments.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
abncommented, Apr 1, 2022

I suspect that’s because of the interpreter used by your notebook kernel. If you set it to your project’s virtualenv, it should work.

You could also try adding jupyter as a dev dependency so that when you run poetry run jupyter the default interpreter is your project’s.

Otherwise, it will reuse the system-wide install of jupyter and whatever python instance it discovers.

0reactions
eliorccommented, Apr 1, 2022

@abn

Seems to work as written in SO, I wanted to write a small version of the real problem but guess that didn’t work well. The real problem, is when you have a Jupyter notebook inside a directory which is not root, lets say

.
├── poetry.lock
├── pyproject.toml
├── README.md
├── notebooks
│   └── my_notebook.ipynb  # <--- This
└── src
    ├── __init__.py
    └── my_module.py

and you’ll open your notebook (even if you launch the Jupyter server with poetry run jupyter notebook) when you will try to import from src it will fail. It happens across my company all the time and that is the problem I want to solve

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using PYTHONPATH — Functional MRI methods
The PYTHONPATH variable has a value that is a string with a list of directories that Python should add to the sys.path directory...
Read more >
Understanding the Python Path Environment Variable in Python
Python path is an environment variable used to maintain directories of custom Python libraries. Learn how to set a python path variable on ......
Read more >
Check and add the module search path with sys.path in Python
In Python, the list of directories searched when importing modules and packages with import , the module search path, is stored in sys.path...
Read more >
Python import, sys.path, and PYTHONPATH Tutorial
The list of directories that it searches is stored in sys.path and can be modified during run-time. To modify the paths before starting...
Read more >
The initialization of the sys.path module search path — Python ...
The PYTHONPATH environment variable is often used to add directories to the search path. If this environment variable is found then the contents...
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