Poetry: ModuleNotFoundError when using poetry run with src package layout

Created on 10 Jan 2020  路  2Comments  路  Source: python-poetry/poetry

  • [x] I am on the latest Poetry version.
  • [x] I have searched the issues of this repo and believe that this is not a duplicate.
  • [x] If an exception occurs when executing a command, I executed it again in debug mode (-vvv option).
  • OS version and name: Ubuntu 18.04
  • Poetry version: 1.0.2

Issue

When using poetry new --src, and attempting to run a script with poetry run, the following exception is produced:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'test_program'

To reproduce, first create a new ubuntu 18.04 docker container:

docker run -it --rm ubuntu:18.04

Then try the following:

# Install required packages 
apt-get update && apt-get install python3 python3-pip python3-venv curl

# Symlink python to python3
# Otherwise, we get a different error that reads '/usr/bin/env: 'python': No such file or directory' later on
ln -s -T /usr/bin/python3 /usr/bin/python

# Install poetry
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python

# Change to home & source poetry env
cd $HOME
source .poetry/env

# Create new poetry project with --src
poetry new --src test-program

# Change to poetry project directory
cd test-program

# Create a new main script to try with `poetry run`
echo -e 'def main(): \n  print("Executed main script")' > src/test_program/app.py

# Add script to pyproject.toml
cat <<EOT >> pyproject.toml

[tool.poetry.scripts]
my-script = "test_program.app:main"
EOT

# Try to run the script, exception is thrown
poetry run my-script

If you move the package out of src/ by doing:

mv src/test_program .

Then poetry run my-script works.

So it seems like there's something it doesn't like about the src/ layout.

Extra poetry debug info:

Poetry
Version: 1.0.2
Python:  3.6.9

Virtualenv
Python:         3.6.9
Implementation: CPython
Path:           /root/.cache/pypoetry/virtualenvs/test-program-Au1Tnuz--py3.6
Valid:          True

System
Platform: linux
OS:       posix
Python:   /usr
Bug

Most helpful comment

That sure does the trick. I think I was confused because my trial project (using the src layout) had no dependencies, so I wasn't expecting to need to run poetry install.

Now I can see it creates a test-program.egg-link file in site-packages under the venv pointing to the src directory, which makes perfect sense.

One helpful thing might be to add to the docs that you always need to run poetry install, regardless of whether there are dependencies or not (probably almost never happens). Entirely possible I just missed it too.

Thanks for the help. 馃憤 From my perspective this issue could be closed.

All 2 comments

Hello @welchr ,

before using poetry run you need to run poetry install. This will install the projects dependency and itself within the venv.

fin swimmer

That sure does the trick. I think I was confused because my trial project (using the src layout) had no dependencies, so I wasn't expecting to need to run poetry install.

Now I can see it creates a test-program.egg-link file in site-packages under the venv pointing to the src directory, which makes perfect sense.

One helpful thing might be to add to the docs that you always need to run poetry install, regardless of whether there are dependencies or not (probably almost never happens). Entirely possible I just missed it too.

Thanks for the help. 馃憤 From my perspective this issue could be closed.

Was this page helpful?
0 / 5 - 0 ratings