Fixed: "Expected a Python module at: src/my_scripts/__init__.py" with uv run
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:
- macOS / Linux
- Windows (PowerShell)
uv init --python 3.15 --vcs none my-scripts
cd my-scripts
rm -r src
uv init --python 3.15 --vcs none my-scripts
cd my-scripts
Remove-Item -Recurse src
--vcs none leaves out the Git repository that uv init would otherwise create. Then I added a script next to pyproject.toml:
print("Hello from hello.py")
And ran it:
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
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

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:
[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:
[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:
uv init --python 3.15 --vcs none --no-package flat-scripts
cd flat-scripts
uv run python main.py
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!

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:
def main() -> None:
print("Hello from my-scripts!")
uv run my-scripts
Built my-scripts @ file:///Users/you/source/my-scripts
Installed 1 package in 3ms
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:
uv run python hello.py
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
| Mistake | What happens | Do this instead |
|---|---|---|
Running the project's command after setting package = false | uv 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 command | Run 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.12 | Those versions made a flat project with main.py. Newer ones make a package with a src folder, which the build needs | Use uv init --no-package for a flat project |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Expected a Python module at: src/<name>/__init__.py | The project is a package, and its module folder was deleted or moved | Set 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-tools | The 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-scripts | The project isn't installed as a package, so its command doesn't exist | Run 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
- Creating projects — what
uv initcreates, with and without a package. - Configuring projects — build systems and
tool.uv.package. - The uv build backend — where the build looks for a package's module.
