Skip to content

Add specified directories to the PYTHONPATH #5390

Closed

Description

@eliorc
  • 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.

Activity

  1. abn commented on Mar 31, 2022

    @abn
    Member

    @eliorc I'm not sure if this (modifying/extending PYTHON PATH) is something poetry will implement. You could consider building a plugin.

    That said, in your SO scenario, simply doing poetry install will suffice as the current project is installed as an editable. And as per your structure this will make src available as a python package, ie. import src, thanks to poetry discovering the package.

    That said, even in cases where you are not interested in packaging, I'd recommend you move the contents of src into src/my_module and add the following to your pyproject.toml.

    packages = [
        { package = "my_module", from = "src" }
    ]

    Your imports in your scripts need to be import my_module.

  2. added and removed
    status/triageThis issue needs to be triaged
    on Mar 31, 2022
  3. eliorc commented on Mar 31, 2022

    @eliorc
    Author

    @eliorc I'm not sure if this (modifying/extending PYTHON PATH) is something poetry will implement. You could consider building a plugin.

    That said, in your SO scenario, simply doing poetry install will suffice as the current project is installed as an editable. And as per your structure this will make src available as a python package, ie. import src, thanks to poetry discovering the package.

    That said, even in cases where you are not interested in packaging, I'd recommend you move the contents of src into src/my_module and add the following to your pyproject.toml.

    packages = [
        { package = "my_module", from = "src" }
    ]

    Your imports in your scripts need to be import my_module.

    poetry install does not solve this when you are executing code not from the main directory

    I actually already implemented this, thought of openning a PR after some testing

    If this is more appropriate as a plugin, I'll see how I can do that

  4. abn commented on Mar 31, 2022

    @abn
    Member

    Here is your SO scenario recreated. Let me know what I am missing here.

    $ tree .
    .
    ├── poetry.lock
    ├── pyproject.toml
    ├── README.md
    ├── scripts
    │   └── my_script.py
    └── src
        ├── __init__.py
        └── my_module.py
    
    2 directories, 6 files
    $ cat pyproject.toml 
    [tool.poetry]
    name = "src"
    version = "0.1.0"
    description = ""
    authors = ["Someone Somewhere <someone@example.com>"]
    readme = "README.md"
    
    [tool.poetry.dependencies]
    python = "^3.10"
    
    
    [build-system]
    requires = ["poetry-core"]
    build-backend = "poetry.core.masonry.api"
    $ poetry install
    Creating virtualenv src in /tmp/python-src/.venv
    Installing dependencies from lock file
    
    Installing the current project: src (0.1.0)
    $ poetry run python scripts/my_script.py 
    src.my_module = <module 'src.my_module' from '/tmp/python-src/src/my_module.py'>
    $ cat scripts/my_script.py 
    from src import my_module
    
    
    if __name__ == "__main__":
        print(f"src.my_module = {my_module}")
    
    $ cd scripts/
    $ poetry run python my_script.py 
    src.my_module = <module 'src.my_module' from '/tmp/python-src/src/my_module.py'>
  5. eliorc commented on Apr 1, 2022

    @eliorc
    Author

    @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

  6. abn commented on Apr 1, 2022

    @abn
    Member

    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.

  7. github-actions commented on Mar 2, 2024

    @github-actions

    This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  8. locked as resolved and limited conversation to collaborators on Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featureFeature requests/implementations

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions