r/learnpython • u/spedgenius • 1d ago
Editable module not found error
I have a custom module that I am developing and I can not get it to import when installing with
"uv pip install -e ."directory structure:
niceachemy/
|-src/
| |-nicealchemy/
| | |-__init__.py
| | |-fields.py
|-pyproject.toml
|-setup.pypyproject.toml:
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "nicealchemy"
version = "0.1"setup.py:
from setuptools import setup, find_packages
setup(name='nice-alchemy',
version='0.1',
author='Nathan Shaffer',
author_email='[email protected]',
license='MIT',
packages=find_packages(where='src'),
package_dir={
'nicealchemy': 'src/nicealchemy',
},
zip_safe=False)script is running on a nicegui docker image that uses uv by default.
When I install my module from github, it works just fine, but if I try to install from a volume as editable, import fails with
ModuleNotFoundError: No module named 'nicealchemy'I have tried specifying the .venv location to pip install and editable compatibility mode
uv pip install --python /opt/venv/bin/python -e . --config-setting editable_mode=compatI have listed the installed modules from within the script and it correctly lists my module:
from importlib.metadata import distributions
installed = sorted(
f"{dist.metadata['Name']}=={dist.version}"
for dist in distributions()
)
for pkg in installed:
print(pkg)/opt/venv/lib64/python3.14 shows a dist-info directory for the module.
I have rebuilt the docker stack many times to make sure the pip install is not being shadowed by the git module installation.
I just can not figure out what I else could possibly check for at this point
EDIT: So what I ended up doing, as nothing I tried doing would work, is I just made a simling in /opt/venv/lib64/python3.14/site-packages to the module src directory, and that worked. I know its not the correct way, but honestly, not sure why the editable install doesn't just do that.
1
u/AdhesivenessFew8441 19h ago
Two things I would check. First, your package_dir mapping does not match the src layout. With find_packages(where='src') the usual setting is package_dir={'': 'src'}, and the custom mapping you have tends to confuse the editable finder even when a normal install works. Second, an editable install writes the absolute path of your project into a .pth file (or an editable finder) inside site-packages, so the path that existed when you ran uv pip install has to exist at that same path when the script runs. If the install happens during the image build and the volume is only mounted at runtime, or is mounted somewhere else, the dist-info is there but the import has nowhere to go. Look inside /opt/venv/lib64/python3.14/site-packages for the .pth file, open it, and confirm that the path in it exists inside the running container.
1
u/spedgenius 5h ago edited 5h ago
I made the changes to the setup.py
from setuptools import setup, find_packages setup(name='nicealchemy', version='0.1', description='Nicegui to fetch sqlalchemy models andlist them', url='https://github.com/nathanjshaffer/itemlist', author='Nathan Shaffer', author_email='[email protected]', license='MIT', packages=find_packages(where='src'), package_dir={'': 'src'}, zip_safe=False)I am running the pip install after the container has started. once I get it working, I will add it to the entrypoint script.
there is _editable_impl_nicealchemy.pth in /opt/venv/lib64/python3.14/site-packages. It has the proper path in it
adding to script:
import sys print(sys.executable)shows that /opt/venv/bin/python is the python environment that is running the script
1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 14h ago
"uv pip install -e ."
If you're already using uv,
You could just run
uv syncwhich automatically installs the project as editable (alongside any dependencies it might have)You don't need
setup.pyas all the metadata should be inpyproject.toml
For example, here I would use
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "nicealchemy"
version = "0.1"
authors = [{ name = "Nathan Shaffer", email = "[email protected]" }]
license = "MIT"and then remove setup.py entirely.
1
u/spedgenius 5h ago
Does removing setup.py work for pip, or just sync?
I tried
uv sync --inexactand still module not found.1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 3h ago
My question is, why are you using
uv pip installhere?I'm also questioning the use of the
--inexactflag here, because that suggests you have dependencies you're not explicitly listing in the project.That being said,
Does removing setup.py work for pip, or just sync?
It should.
1
u/spedgenius 3h ago
the docker image that I am using manages python with uv. That is an upstream decision. I am not familiar with uv, so was not aware of uv sync vs uv pip install. The docker image comes with a load of preinstalled and necessary python packages. when I run uv sync to add my module, without --inexact, it uninstalls all of the necessary packages.
1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 3h ago
I see. Presumably that means you're also not using a virtual environment, then?
You didn't share your logs, so I'm going in blind here, but when running
uv sync --inexactI'd expect you to see something similar to this (running this example on my Rust/Python project template, default project name isproject-namewhich it installs as editable - you can ignore the warning):PS E:\GitHub\python-rust-template> uv sync Resolved 42 packages in 1ms Built project-name @ file:///E:/GitHub/python-rust-template Prepared 2 packages in 17.10s Uninstalled 2 packages in 11ms ░░░░░░░░░░░░░░░░░░░░ [0/2] Installing wheels... warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance. If the cache and target directories are on different filesystems, hardlinking may not be supported. If this is intentional, set `export UV_LINK_MODE=copy` or use `--link-mode=copy` to suppress this warning. Installed 2 packages in 285ms ~ project-name==0.1.0 (from file:///E:/GitHub/python-rust-template) - ruff==0.17.4 + ruff==0.17.5If this is not the case, I'd first look at your
uvsettings (either global ones, if they're configured in the Docker image itself, or whatever you may have jotted down in yourpyproject.toml) and possibly environment variables. ParticularlyUV_NO_INSTALL_PROJECTorUV_NO_INSTALL_WORKSPACE: https://docs.astral.sh/uv/reference/environment/#uv_no_install_projectWhen in doubt, you can run
uv run --show-settingsto see all the configuration optionsuvis currently configured to use.1
u/spedgenius 3h ago
running uv sync --inexact gives a similar output the shows the module built and installed.
all this time I have been assuming the docker image was not using a venv, but I just looked through the image envvars and found
UV_PROJECT_ENVIRONMENT /opt/venv So then I am assuming I need to activate the venv before installing? but from what I can see there is no command to do so, so I need to cd to the venv directory? but then how do I install/sync a module from a different directory?
1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 1h ago
No,
uvshould manage the virtual environment automatically if there is one. There's no need to manually activate anything.I was making the assumption because generally in Docker images it's not necessary to have a separate virtual environment. Either way that shouldn't really matter.
2
u/EszterSantos72 20h ago
In setup.py, set package_dir={"": "src"} and ensure packages=find_packages(where="src") so that Python correctly locates your nicealchemy module inside the src directory during editable installs.