5
submitted 11 months ago* (last edited 11 months ago) by breadcrumb@lemmy.world to c/python@programming.dev

I want to build a python package using setuptools. The folder structure of the project is the following (some non-essential parts have been deleted):

energy-monitor
├── config
│ ├── config.yml
│ └── secrets.yml
├── data
│ └── cpu_tdp.json
├── energy_monitor
│ ├── core
│ │ ├── gui.py
│ │ ├── __init__.py
│ ├── data
│ │ └── tableExport.json
│ ├── __init__.py
│ ├── main.py
│ └── utils
│     ├── api_calls.py
│     └── __init__.py
├── energy-monitor.py
├── LICENSE
├── MANIFEST.in
├── pyproject.toml
├── README.md
└── requirements.txt

The content of the pyproject.toml file is the following (some non-essential parts have been deleted):

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

[project]
name = "energy_monitor"
version = "0.0.1"
description = "Energy monitor"
readme = "README.md"
requires-python = ">=3.11"
license = {text = "GPLv3"}
classifiers = [
  "Programming Language :: Python :: 3",
  "Operating System :: OS Independent",
]
dynamic = ["dependencies"]

[tool.setuptools.dynamic]
dependencies = {file = ["requirements.txt"]}

[tool.setuptools]
packages = [
  "energy_monitor", 
  "energy_monitor.core", 
  "energy_monitor.utils"
]
include-package-data = true

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

Finally, the content of the MANIFEST.in file is the following:

include README.md
include LICENSE
graft config

I generate the package with python -m build and install the .tar.gz archive with pipx. According to setuptools documentation, I expect to find my config folder, together with README and LICENSE in the interpreter directory (site-packages) after the installation. However, this doesn't happen and I cannot run the app becase it complains that it doesn't find the config. What am I missing?

[-] 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!

[-] breadcrumb@lemmy.world 1 points 1 year ago

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.

4
submitted 1 year ago* (last edited 1 year ago) by breadcrumb@lemmy.world to c/python@programming.dev

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
[-] breadcrumb@lemmy.world 4 points 1 year ago* (last edited 1 year ago)

As many, I fit the description except for the age, but I hope this monoculture thing goes away. I don't want an entire social network to be a huge bubble. If I want a bubble I join one of the many communities populated by people similar to me, but I want to have the chance to look "for something completely different", getting in touch with world views completely opposte to mine.

breadcrumb

joined 1 year ago