Skip to main content

Fixed: "Expected a Python module at: src/my_scripts/__init__.py" with uv run

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

uv run python hello.py stopped with Failed to build and Expected a Python module at: src/my_scripts/__init__.py. The project was made with uv init, which sets it up as an installable package, and its src folder had been deleted. Telling uv that the project isn't a package fixed it.

What I Was Doing​

I was setting up a small uv project to hold one-file Python scripts. uv init created it with a src folder that the scripts didn't use, so I deleted the folder:

Terminal
uv init --python 3.15 --vcs none my-scripts
cd my-scripts
rm -r src

--vcs none leaves out the Git repository that uv init would otherwise create. Then I added a script next to pyproject.toml:

hello.py
print("Hello from hello.py")

And ran it:

Terminal
uv run python hello.py

What I Expected​

Hello from hello.py. The script imports nothing from the project, so the deleted folder shouldn't matter to it.

What I Got​

Output
Using CPython 3.15.2 interpreter at: /Library/Frameworks/Python.framework/Versions/3.15/bin/python3.15
Creating virtual environment at: .venv
error: Failed to build `my-scripts @ file:///Users/you/source/my-scripts`
cause: Expected a Python module at: src/my_scripts/__init__.py

The terminal after uv run python hello.py: uv used CPython 3.15.2 and created .venv, then error: Failed to build my-scripts, cause: Expected a Python module at: src/my_scripts/__init__.py.

uv created the project's environment, then tried to build the project itself, and stopped. The script never ran.

What I Checked​

1. What uv init had made​

Before I deleted it, the src folder held one file, src/my_scripts/__init__.py. The project's pyproject.toml still points at it:

pyproject.toml
[project]
name = "my-scripts"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
authors = [
{ name = "Your Name", email = "you@example.com" }
]
requires-python = ">=3.15"
dependencies = []

[project.scripts]
my-scripts = "my_scripts:main"

[build-system]
requires = ["uv_build>=0.13.10,<0.14.0"]
build-backend = "uv_build"

Two tables matter. [project.scripts] gives the project a command, my-scripts, that runs the main function in the my_scripts module. [build-system] says how to build the project into an installable package: with uv_build, uv's own build backend. The authors line comes from your Git name and email, so yours shows those.

2. What uv's documentation says about uv init​

The project creation guide says that applications are "the default target for uv init", and that "The source code lives in a src directory with a module directory and an __init__.py file". It also says: "A build system is defined, so the project will be installed into the environment". And in a note: "Prior to v0.12, uv did not define a build system for applications by default." Guides written for older uv show a main.py instead of a src folder.

3. What uv does with a build system​

The project configuration page says: "If a build system is defined, uv will build and install the project into the project environment." uv run does that before it runs anything, even a script that doesn't need the package. The build backend page says where the build looks: "By default, a single root module is expected at src/<package_name>/__init__.py." For my-scripts, that's src/my_scripts/__init__.py: the file I deleted.

Why It Happens​

uv init made my-scripts an installable package, so every uv run builds and installs it first. The build needs the package's code in src/my_scripts/__init__.py. With the folder gone, the build fails, and uv run stops before it reaches hello.py.

The Fix​

This project is only for scripts, so it doesn't need to be a package. uv's configuration page has a setting for that: "Setting tool.uv.package = false will force a project package not to be built and installed into the project environment." Add these lines at the end of pyproject.toml:

pyproject.toml (the end)
[tool.uv]
package = false

[tool.uv] holds uv's own settings for the project, and package = false tells uv not to build or install it.

Another way: start the project without a package​

When a new project is only for scripts, ask uv init for a flat one with --no-package:

Terminal
uv init --python 3.15 --vcs none --no-package flat-scripts
cd flat-scripts
uv run python main.py
Output
Initialized project `flat-scripts` at `/Users/you/source/flat-scripts`
Using CPython 3.15.2 interpreter at: /Library/Frameworks/Python.framework/Versions/3.15/bin/python3.15
Creating virtual environment at: .venv
Hello from flat-scripts!

The terminal after uv init --no-package flat-scripts and uv run python main.py: the project was initialized, uv created .venv with CPython 3.15.2, and main.py printed Hello from flat-scripts!.

This project has a main.py next to pyproject.toml and no src folder, and its pyproject.toml has no [build-system] or [project.scripts]. uv's guide describes it: "The project is not a package and will not be installed into the environment".

Another way: put the module back​

If you do want the package, for example for its my-scripts command, bring the file back instead. Remove the [tool.uv] lines if you added them, and create src/my_scripts/__init__.py again, with the main function the command runs:

src/my_scripts/__init__.py
def main() -> None:
print("Hello from my-scripts!")
Terminal
uv run my-scripts
Output
Built my-scripts @ file:///Users/you/source/my-scripts
Installed 1 package in 3ms
Hello from my-scripts!

The terminal after uv run my-scripts with the module back: uv built my-scripts, installed 1 package, and the command printed Hello from my-scripts!.

uv built and installed the package, and its command ran. uv run python hello.py worked again too, and printed Hello from hello.py.

After the Fix​

With package = false added, run the script again:

Terminal
uv run python hello.py
Output
Hello from hello.py

The terminal after the fix: uv run python hello.py printed Hello from hello.py.

The script ran. uv no longer builds the project, so the missing folder doesn't matter. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Running the project's command after setting package = falseuv run my-scripts stopped with error: Failed to spawn: my-scripts and cause: No such file or directory (os error 2): without the package, nothing installs the commandRun the scripts with uv run python <file>, and delete the [project.scripts] table, which names a module that no longer exists
Following a guide written for uv older than 0.12Those versions made a flat project with main.py. Newer ones make a package with a src folder, which the build needsUse uv init --no-package for a flat project

Troubleshooting​

SymptomLikely causeFix
Expected a Python module at: src/<name>/__init__.pyThe project is a package, and its module folder was deleted or movedSet package = false under [tool.uv], or put the module back
Expected a Python module at: src/my_tools/__init__.py after renaming the project from my-scripts to my-toolsThe build looks for a module named after the project. uv's build backend page: "the package name is lowercased and dots and dashes are replaced with underscores"Rename the folder under src to match the new name
error: Failed to spawn: my-scriptsThe project isn't installed as a package, so its command doesn't existRun the file with uv run python <file>

FAQ​

Which fix should I choose?​

For a folder of scripts, package = false in an existing project, or uv init --no-package for a new one. When you want the project's own command, or plan to share the project as a package, keep the package and put its module back.

Why does uv run python hello.py build a package it doesn't use?​

uv's page on running commands explains: "When using run, uv will ensure that the project environment is up-to-date before running the given command." For a project with a build system, being up to date includes having the project itself installed.

Conclusion​

Expected a Python module at: src/my_scripts/__init__.py meant that uv was building the project as a package, and the package's code had been deleted. package = false under [tool.uv] stopped uv from building it, and uv run python hello.py printed its greeting. uv init --no-package avoids the problem in a new project, and putting the module back fixes it when you want the package.

Additional Resources​