nicomp
nicomp

Reputation: 4647

Package installed from test.pypi.org is not the same folder structure as was uploaded to the repository

I created a package and uploaded it to test.pypi.org: https://test.pypi.org/project/name-generator-nicomp/0.0.2/

The logic reads a text file that I want to include in the package. In my initial design I put the text file in its' own folder and my logic ran just fine on my local machine.

I installed the package from test.pypi.org onto a different computer using pip:

python3 -m pip install --index-url https://test.pypi.org/simple/ name-generator-nicomp --upgrade

Then, I run this script:

if __name__ == "__main__":
    from name_generator_nicomp.NameGenerator import NameGenerator
    nameGenerator = NameGenerator()

and it fails inside the package code on this line:

    with importlib.resources.open_text("src.data", self.noun_file) as my_file:

The error is:

ModuleNotFoundError: No module named 'src'

Looking in the .gz file created by the build script, the src folder is there and the data folder is inside it. The repo on test.pypi.org also has the folder structure in it. It can be downloaded and verified here: https://test.pypi.org/project/name-generator-nicomp/0.0.2/#files

However, when I look at the installation of the package on my local machine, all the files (code and data) are in one folder and there are no other folders at all.

My .toml file is

[project]
name = "name_generator_nicomp"
version = "0.0.2"
authors = [
  { name="xxx xxxxxx", email="xxx.com" },
]
description = "Some description."
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]

[project.urls]
Homepage = "https://github.com/nicomp42/name_generator"
Issues = "https://github.com/nicomp42/name_generator/issues"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

The entire traceback is:

Traceback (most recent call last):
  File "C:\Users\nicholdw\eclipse-workspace\myNameGenerator\mainPackage\main.py", line 3, in <module>
    nameGenerator = NameGenerator()
  File "C:\Users\nicholdw\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\name_generator_nicomp\NameGenerator.py", line 36, in __init__
    self.prepare()
  File "C:\Users\nicholdw\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\name_generator_nicomp\NameGenerator.py", line 47, in prepare
    with importlib.resources.open_text("src.data", self.noun_file) as my_file:
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\importlib\resources.py", line 82, in open_text
    open_binary(package, resource), encoding=encoding, errors=errors
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\importlib\resources.py", line 43, in open_binary
    package = _common.get_package(package)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\importlib\_common.py", line 66, in get_package
    resolved = resolve(package)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\importlib\_common.py", line 57, in resolve
    return cand if isinstance(cand, types.ModuleType) else importlib.import_module(cand)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'src'

Upvotes: 0

Views: 91

Answers (1)

Timur U
Timur U

Reputation: 445

  1. Its need to add package data information to pyproject.toml:
[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.package-data]
"*" = ["*.txt"]
  1. When you're calling data at NameGenerator.py:
with importlib.resources.open_text("src.data", "nouns.txt") as my_file:

it wrong, because src is a src-like source python folder, but not the package!

This is returning an error:

ModuleNotFoundError: No module named 'src'

You're set three packages: data, main, name_generator_nicomp. There is its need to invoke the package by name. In your case its data.

I think the best way is:

import name_generator_nicomp

. . . 

with importlib.resources.open_text("name_generator_nicomp.data", "nouns.txt") as my_file:
  1. There is the excellent documentation:

There a lot samples to any situations. Good luck with your work!!!

Upvotes: 0

Related Questions