Skip to content
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

ModuleNotFoundError when installing wheel from own local python package #390

Closed
7 tasks done
telegott opened this issue May 10, 2022 · 5 comments
Closed
7 tasks done
Labels
question Question or problem

Comments

@telegott
Copy link

telegott commented May 10, 2022

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Typer but to Click.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

# file tree

├── commands
│   ├── a.py
│   ├── __init__.py
│   └── b.py
├── dist
│   ├── tool-0.1.0-py3-none-any.whl
│   └── tool-0.1.0.tar.gz
├── __init__.py
├── poetry.lock
├── pyproject.toml
├── README.md
├── tests
│   ├── __init__.py
│   └── test_tool.py
└── tool
    ├── __init__.py
    ├── main.py


# tool/main.py

import typer

from commands import a, b

app = typer.Typer()
app.add_typer(a.app, name='a')
app.add_typer(b.app, name='b')

# section from pyproject.toml

[tool.poetry.scripts]
tool = "tool.main:app"

Description

My goal is to have a command line tool that's available everywhere and that mostly wraps other CLI programs.

I followed the example guide on how to create typer programs and how to build a package from it. For now, it should be installed from a local wheel using pipx. poetry install, poetry build and pipx install ... all work, it's found in the PATH afterwards, but if I try to run it I get

Traceback (most recent call last):
  File "/home/michael/.local/bin/tool", line 5, in <module>
    from tool.main import app
  File "/home/michael/.local/pipx/venvs/tool/lib/python3.10/site-packages/tool/main.py", line 3, in <module>
    from commands import a, b
ModuleNotFoundError: No module named 'commands'

I have the __init__.py in commands/, but it seems to mess that up in the process somehow, it does not copy these files into the directory it reports for the pipx venv path. Has anyone experienced something like this?

Operating System

Linux

Operating System Details

Linux Mint 20

Typer Version

0.4.1

Python Version

3.10.2

Additional Context

No response

@telegott telegott added the question Question or problem label May 10, 2022
@brizzbuzz
Copy link

Yea, it seems like typer is super rigid on the exact directory structure here. I'm running into the same issues with practically any modifications to the directory structure listed in the example here

In order to get it to work, I had to make my module match the app name, and had to move all of my commands into the top level module, which is not really a scalable solution :(

@brizzbuzz
Copy link

Though, it seems like this is possibly more of a poetry question than a typer question? not sure ultimately how much control typer has over the install process.

@telegott
Copy link
Author

telegott commented May 12, 2022

@unredundant I was able to get it to work with all relevant files being (nested) inside a subdirectory which hosts main.py:

├── tests
│   ├── __init__.py
│   └── test_helpers.py
└── werkzeug
    ├── commands
    │   ├── one.py
    │   ├── __init__.py
    │   └── other.py
    ├── helpers.py
    ├── __init__.py
    ├── main.py
    ├── paths.py

If I understand you correctly you got it only to work with a flat file structure (no subdirectories)? For me it works with the shown structure!
It also helped uninstalling the package with pipx before another attempt, otherwise it didn't pick up changes

@brizzbuzz
Copy link

Interesting... I'll have to play around with this more. Glad you got it working :) means I can't be far off

@vinigfer
Copy link

Took me a while to get it working for me, but managed alright! My project structure looks like this:

vinicius.ferreira@MyPC ep2023 % tree                
.
├── README.md
├── dist
│   ├── ep2023-0.1.0-py3-none-any.whl
│   └── ep2023-0.1.0.tar.gz
├── ep2023
│   ├── __init__.py
│   ├── git_constants.py
│   ├── main.py ------------------> Calls services, team_product1 and team_product2
│   ├── services.py   ------------> Doesn't call anybody
│   ├── team_product1.py  -------> Calls services
│   └── team_product2.py   ------> Calls services
├── poetry.lock
├── pyproject.toml
└── tests
    └── __init__.py

Had to change pyproject.toml like this:

[tool.poetry]
# Add the lines below
packages = [
    { include = "*.py", from = "ep2023" },
]

[tool.poetry.scripts]
ep2023 = "main:app"

That way after installing my package with pip install dist/ep2023-0.1.0.tar.gz, I can that use ep2023 + [TAB] without the ModuleNotFoundError. Hope this helps anyone who is having this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
Projects
None yet
Development

No branches or pull requests

3 participants