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 immediately after initializing a project #7622

Open
5183nischal opened this issue Sep 22, 2024 · 7 comments
Open

ModuleNotFoundError immediately after initializing a project #7622

5183nischal opened this issue Sep 22, 2024 · 7 comments
Labels
needs-mre Needs more information for reproduction

Comments

@5183nischal
Copy link

The following error occurs when I initialize a project under ~/Documents but not say under ~/Downloads (in macos).

  1. Initialize a new project using uv:
    I initialized a new project named trials using the uv command.

    uv init --lib trials

    Output:

    Initialized project `trials` at `/Users/nisch/Documents/trials`
  2. Navigate to the project directory:
    I changed the directory to the newly created trials project.

    cd trials
  3. Run Python command to import the trials module:
    I attempted to run a Python command to import the trials module using uv.

    uv run python -c "import trials"

    Output:

    Using Python 3.10.15
    Creating virtual environment at: .venv
        Built trials @ file:///Users/nisch/Documents/trials
    Installed 1 package in 1ms
  4. Run the same command again:
    I ran the same command again to import the trials module.

    uv run python -c "import trials"

    Output:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    ModuleNotFoundError: No module named 'trials'

Issue

After initializing the project and running the command to import the trials module, I encountered a ModuleNotFoundError. It seems that the trials module is not being recognized, even though the project was initialized successfully.

Expected Behavior

The trials module should be importable after initializing the project and running the command.

Actual Behavior

The trials module is not found, resulting in a ModuleNotFoundError.

Verbose Output

I ran the command with the -v (verbose) flag to get more detailed output:

uv run -v python -c "import trials"

Verbose Output:

DEBUG uv 0.4.15
DEBUG Found project root: `/Users/nisch/Documents/trials`
DEBUG No workspace root found, using project root
DEBUG Discovered project `trials` at: `/Users/nisch/Documents/trials`
DEBUG Reading requests from `/Users/nisch/Documents/trials/.python-version`
DEBUG The virtual environment's Python version satisfies `Python 3.10`
DEBUG Using request timeout of 30s
DEBUG Found static `pyproject.toml` for: trials @ file:///Users/nisch/Documents/trials
DEBUG No workspace root found, using project root
DEBUG Existing `uv.lock` satisfies workspace requirements
Resolved 1 package in 4ms
DEBUG Using request timeout of 30s
WARN  Failed to read metadata for file: No such file or directory (os error 2)
WARN  Failed to read metadata for file: No such file or directory (os error 2)
DEBUG Requirement already installed: trials==0.1.0 (from file:///Users/nisch/Documents/trials)
Audited 1 package in 0.39ms
DEBUG Using Python 3.10.15 interpreter at: /Users/nisch/Documents/trials/.venv/bin/python3
DEBUG Running `python -c import trials`
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'trials'
DEBUG Command exited with code: 1

Thanks in advance!

@choucavalier
Copy link

👍 I got this problem often as well

@charliermarsh
Copy link
Member

If you want the template project to be created with an importable directory like that you, consider using uv init trials --lib. By default, we create an --app, which just contains runnable scripts as opposed to a buildable project. \cc @zaneib

@charliermarsh charliermarsh added the question Asking for clarification or support label Sep 22, 2024
@5183nischal
Copy link
Author

If you want the template project to be created with an importable directory like that you, consider using uv init trials --lib. By default, we create an --app, which just contains runnable scripts as opposed to a buildable project. \cc @zaneib

But I do initialize it with the --lib!

@charliermarsh
Copy link
Member

Oh sorry -- I missed that. I can't reproduce this though.

@charliermarsh charliermarsh added needs-mre Needs more information for reproduction and removed question Asking for clarification or support labels Sep 22, 2024
@5183nischal
Copy link
Author

Oh sorry -- I missed that. I can't reproduce this though.

Yes, I can't reproduce this everywhere in my own computer either. Only when I'm under ~/Documents in my computer I get this error. Want to understand if you guys would have an idea why that might be happening.

@cyberNKYX
Copy link

I'm experiencing the same issue on windows, a different folder with an existing project migrated from pdm. It looks like the local package was not installed under some condition before running the command.

I also tried do it manually by running uv add --dev --editable . at first, then manually enter the virtual environment ./.venv/Scripts/activate, still the local module was not found.

But when I deleted pyproject.toml and reinitialized the project uv init --lib and ran uv sync before copying the existing dependencies list to pyproject.toml, it started working. And afterwards, I copied the existing dependencies and it worked as usual.

@Anselmoo
Copy link

I tried to change from poetry to uv. Formally, with tool.poetry.scripts it is working fine; via project.scripts, I got also ModuleNotFoundError: No module named 'tanabesugano'

However, I used uvx migrate-to-uv for the migration.

Anselmoo/TanabeSugano#131

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-mre Needs more information for reproduction
Projects
None yet
Development

No branches or pull requests

5 participants