Skip to content

ModuleNotFoundError for poetry script #3928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
anthonyatp opened this issue Apr 12, 2021 · 13 comments
Closed

ModuleNotFoundError for poetry script #3928

anthonyatp opened this issue Apr 12, 2021 · 13 comments
Labels
kind/bug Something isn't working as expected

Comments

@anthonyatp
Copy link

  • OS version and name: macOS 11.0.1 (M1 mac)
  • Poetry version: 1.14

Issue

I am running a script in a sub dir - however, when it runs, it doesn't recognise local modules.

This is related to #1868 - however poetry install doesn't resolve my issue.

Here is a rough layout of the app:

/src
  pyproject.toml
  /blueprints
    __init__.py
    /foo
      a.py
      utils.py
      __init__.py

a.py

from blueprints.foo.utils import util

def demo_script():
  data = util()
   ...

pyproject.toml

[tool.poetry.scripts]
demo_script = "src.blueprints.foo.a:demo_script"

But when I run it using poetry run demo_script, it can't find the local modules:

  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File ".../src/blueprints/foo/a.py", line 1, in <module>
    from blueprints.foo.utils import util
ModuleNotFoundError: No module named 'blueprints'

Please let me know if you need any further info.

Thanks!

@anthonyatp anthonyatp added kind/bug Something isn't working as expected status/triage This issue needs to be triaged labels Apr 12, 2021
@finswimmer
Copy link
Member

Hello @anthonyatp,

[tool.poetry.scripts]
demo_script = "src.blueprints.foo.a:demo_script"

the src. is wrong. It must be:

[tool.poetry.scripts]
demo_script = "blueprints.foo.a:demo_script"

@anthonyatp
Copy link
Author

Hi @finswimmer,

I did initially have it as:

[tool.poetry.scripts]
demo_script = "blueprints.foo.a:demo_script"

However, when I do, it doesn't allow me to run the script for the same reason:

File "<string>", line 1, in <module>
  File "/opt/homebrew/opt/[email protected]/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'blueprints'

@clausjuhl
Copy link

I have the same problem.

@grlscz
Copy link

grlscz commented Nov 26, 2021

Windows 10, python 3.9, poetry 1.1.11 in my case.

For a tool.poetry.script, poetry creates 2 files:

  1. script_name which is a python file starting with from my_module import my_function
  2. script_name.cmd which contains "path\to\poetry\env\Scripts\python.exe" "%~dp0\script_name" %*

Running script_name leads to the aforementioned ModuleNotFoundError.
The same goes for running "path\to\poetry\env\Scripts\python.exe" "path\to\poetry\env\Scripts\script_name" directly.

However "path\to\poetry\env\Scripts\python.exe" < "path\to\poetry\env\Scripts\script_name" runs fine.

@tomagb
Copy link

tomagb commented Jan 4, 2022

I'm hitting the same problem and I'm curious if this is a bug and I can try to raise a PR or there is something I don't understand about how I should use a package with more python files in it. It seems a basic need, though.
Also, might be there any workaround for it?

@kmilo9999
Copy link

Hello,

I have the same issue. The folder structure of my project is:

/my-image-converter
  pyproject.toml
  /tiff_converter
    file1.py
    file2.py

file1 imports a function from file2,

file1.py

import image_process_helper as iph
...
...
...
  iph.equalize_image_histogram_opencv
...
...

This is my pyproject.toml

[tool.poetry]
name = "my-project"
version = "0.1.0"
description = "Tool to convert stack of tiff images to a png texture map"
authors = ["Camilo Diaz <[email protected]>"]
license = "MIT"

packages = [
    { include = "tiff_converter/file2.py" },
]

[tool.poetry.dependencies]
python = ">=3.7,<3.11"
opencv-contrib-python = "4.5.4.60"
black = "^22.3.0"
tiffile = "^2018.10.18"
pyseq = "^0.5.5"
scipy = "1.7.3"
typer = "^0.4.1"
imagesize = "^1.3.0"
matplotlib = "^3.5.1"


[tool.poetry.dev-dependencies]



[tool.poetry.scripts]
convert2png="tiff_converter.file1:main"

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

When I do :
poetry run convert2png

I get the error:


Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "F:\my-image-converter\tiff_converter\file1.py", line 10, in <module>
    import file2 as iph
ModuleNotFoundError: No module named 'file2'

Any ideas on what am I missing?

@abn
Copy link
Member

abn commented Apr 17, 2022

@kmilo9999 you need to make the following changes.

  1. Add an empty file at tiff_converter/__init__.py.
  2. Either remove the packages = option entirely from the pyproject.toml file or fix it to be packages = [{include = "tiff_converter"}] as this needs to be the package directory not the python file.

@abn
Copy link
Member

abn commented Apr 17, 2022

@anthonyatp based on your last response I think your layout is problematic.

If you have an src directory at the same level as your toml file then, you need packages = [{include = "blueprints", from = "src"}].

Closing this issue as this is a configuration issue.

@abn abn removed the status/triage This issue needs to be triaged label Apr 17, 2022
@abn abn closed this as completed Apr 17, 2022
@alastairtree
Copy link

I had the exact same problem and could not get any of the solutions to work. Found a work around using a init.py file containing import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__))) and that fixed the module path for the poetry script. Credit to https://stackoverflow.com/a/49375740/3140853

@jamesdbrock
Copy link

I had the exact same problem and could not get any of the solutions to work.

@epogrebnyak
Copy link

epogrebnyak commented May 27, 2023

I'm hitting the same issue with no clue why imports do not work in a very standard setup.

Here is what docs say:

[tool.poetry.scripts]
my_package_cli = 'my_package.console:run'

Here is what I have:

[tool.poetry.scripts]
abacus_cli = 'abacus.cli:entry_point'

I get the following error, when I run poetry run abacus_cli:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/python/3.10.4/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1001, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'abacus.cli'; 'abacus' is not a package

abacus is installed - it is a package indeed, the following code passes:

poetry run python -c "import abacus"

@finswimmer
Copy link
Member

Hey,

this issue is long closed. Please open a new issue with a reproducer.

Thanks a lot.

fin swimmer

Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 29, 2024
DrearyWillow referenced this issue in DrearyWillow/pdfp Jul 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests

10 participants