Home > other >  How to make scripts installed with pip from pyproject.toml file see imports?
How to make scripts installed with pip from pyproject.toml file see imports?

Time:10-04

I'm trying to learn how to package scripts with poetry to be later installed via pip.

Here's a sample minimal pyproject.toml

name = "scripttest"
version = "0.1.0"
description = ""
authors = []
packages = [ 
    { include = "src" }
]


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


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

[tool.poetry.scripts]
scripttest = 'src.scripttest:main'

Repository has following structure

├── pyproject.toml
├── README.md
└── src
    ├── importantimport.py
    └── scripttest.py

scripttest.py only has an import and a print statement

from importantimport import data
def main():
    print(data)

while importantimport.py contains a list inside

data = [0, 1, 0, 1]

Running python src/scripttest.py yields expected output of
[0, 1, 0, 1], but once installed via pip install ., running scripttest returns ModuleNotFoundError: No module named 'importantimport', despite importantimport.py being right next to it in ~/.local/lib/python3.10/site-packages/src

How do I ensure that imports work locally after pip install?

Thank you.

CodePudding user response:

Delete these lines:

packages = [ 
    { include = "src" }
]

Your project has no packages, only lone modules. Poetry's default discovery patterns will find the importantimport.py file.

Change these lines:

[tool.poetry.scripts]
scripttest = 'src.scripttest:main'

Into this:

[tool.poetry.scripts]
scripttest = 'scripttest:main'

CodePudding user response:

I think you will want

from scripttest.importantimport import data

This is because importantimport is package with your library, scripttest

  • Related