Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying Poetry's virtual environment PYTHONPATH

I'm using Poetry to manage dependencies - NOT as a packaging tool. Given the following layout

.
├── poetry.lock
├── pyproject.toml
├── src
│   ├── __init__.py
│   └── my_module.py
└── scripts
    └── my_script.py

Inside my_script.py I'm using from src.my_module import MyClass but when I call poetry run python scripts/my_script.py I get:

ModuleNotFoundError: No module named 'src'

I want to add the root directory to the PYTHONPATH of this specific environment, but I want to avoid manually exporting it everytime.

Bottom line I'm looking for Poetry's equivalent for vitualenvwrappers add2virtualenv

Is this possible under Poetry?

like image 303
bluesummers Avatar asked Feb 27 '26 10:02

bluesummers


1 Answers

You can put your my_module.py directly into the src folder, but this is not recommended. Create a package folder first which then contains all your modules.

project
├── poetry.lock
├── pyproject.toml
├── scripts
│   └── my_script.py
└── src
    └── project
        ├── __init__.py
        └── my_module.py

With a pyproject.toml like this:

[tool.poetry]
name = "project"
version = "0.1.0"
description = ""
authors = ["finswimmer <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.9"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

Don't forget to run poetry install once. This will install the project in editable mode.

like image 153
finswimmer Avatar answered Mar 01 '26 23:03

finswimmer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!