this post was submitted on 17 Jul 2023
4 points (100.0% liked)

Python

6400 readers
18 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS
 

I'm writing a python package that I would like to distribute as a standalone terminal app. The structure of the project folder is the following:

energy-monitor/
-- config/
-- doc/
-- tests/
-- energymonitor/
---- init.py -> (empty)
---- main.py -> def main()
---- data/
---- ..other packages..
-- project.toml

I'm using setuptools to generate a .tar.gz archive, some relevant parts of the project.toml file are:

[build-system]
requires = ["setuptools>=68.0"]
build-backend = "setuptools.build_meta"

[project]
name = "energy-monitor"
version = "0.0.1"

...

[tool.setuptools.packages.find]
where = ["energymonitor"]

[tool.setuptools.package-data]
data = ["data/*"]

[project.scripts]
energy-monitor = "energymonitor.main:main"

I generate the .tar.gz and the .whl files with the command python -m build, then I run pipx install path/to/energy-monitor.tar.gz. The installation is succesful, but when calling energy-monitor from the command line I get:

Traceback (most recent call last):
  File "/home/mattia/.local/bin/energy-monitor", line 5, in <module>
    from energymonitor.main import main
ModuleNotFoundError: No module named 'energymonitor'

Why is this happening? I was not able to find any helpful solution online. It's the first that I build a python package so sorry if the issue is trivial.

  • python version: 3.11.3
you are viewing a single comment's thread
view the rest of the comments
[–] Andy@programming.dev 1 points 1 year ago (1 children)

When installed, is the module name actually energy_monitor, instead of energymonitor?

To investigate interactively, you could create and activate a venv, install the package from the archive, install ipython, run that, and use its tab completion to import energ<TAB>.

[–] breadcrumb@lemmy.world 1 points 1 year ago (1 children)

I tried to change both the project name, which was energy-monitor, and the package name (energymonitor) to be the same and I set both to energy_monitor, but nothing changes...but if I open the python shell in the same folder as the project I can import the energy_monitor package with no errors, as soon as I change folder it doesn't find the package anymore. It looks like it didn't install the package system wide, but I thought that pipx should handle these kind of things.

[–] Andy@programming.dev 1 points 1 year ago (1 children)

Pipx is for making the script runnable system wide, not making the code importable system wide.

Please go to a different folder, create and activate a venv, install the package and ipython, and see what you can import.

[–] breadcrumb@lemmy.world 2 points 1 year ago* (last edited 1 year ago)

Yeah sorry I expressed myself wrongly, I mean that it looked like pipx didn't install the package in the dedicated venv, and that was actually the case because I didn't specify which packages to install in the pyproject.toml file apparently. I substituted these lines:

[tool.setuptools.packages.find]
where = ["energymonitor"]

[tool.setuptools.package-data]
data = ["data/*"]

with these lines:

[tool.setuptools]
packages = ["energymonitor"]
include-package-data = true

and it worked!