Lawhatre
Lawhatre

Reputation: 1450

Custom python packages gives ModuleNotFoundError while importing

Following is my python package structure

pkg
|-- src
    |-- data
        |-- __init__.py
        |-- loader1.py
    |-- dataset
        |-- __init__.py
        |-- loader2.py
    |-- utils
        |-- __init__.py
        |-- chk.py
|-- setup.py
|-- __init__.py
|-- LICENSE
|-- README.md

After pip installation I wanted to use from pkg.data.loader1 import func and so I used from pkg.data.loader1 import func (in linux terminal). As a result, I got ModuleNotFoundError: No module named in response.

How can I fix this.

Edit: setup.py

from setuptools import setup

with open("README.md", 'r') as fh:
    long_description = fh.read()

setup(
    name="pkg",
    version="0.0.1",
    description="will add",
    long_description=long_description,
    long_description_content_type="text/markdown",
    author="my name",
    packages=['pkg'],
    install_requires=[]
    )

Upvotes: 0

Views: 103

Answers (2)

Arne
Arne

Reputation: 20157

Looking at the structure of your import statements, what you need is probably an additional folder holding all your sub-packages:

pkg
├── src  # important: its only content is pkg
│   └── pkg  # new folder here
│       ├── __init__.py  # important: this file needs to exist
│       ├── data
│       │   ├── __init__.py
│       │   └── loader1.py
│       ├── dataset
│       │   ├── __init__.py
│       │   └── loader2.py
│       └── utils
│           ├── __init__.py
│           └── chk.py
├── setup.py
├── __init__.py
├── LICENSE
└── README.md

Next, your setup.py needs to know that src is the code source, but not the package root, which is the folder pkg right below it, plus all its subpackages:

setup(
    ....
    package_dir={"": "src"},
    packages=find_packages("pkg"),
    ....
)

You can then install this package into your currently active python interpreter with python -m pip install -e . in a way that you don't need to re-install it after every code change (you still need to reinstall it after updating setup.py though).

Upvotes: 1

Jeet Patel
Jeet Patel

Reputation: 1241

Do you have package_dir={"": "pkg"}, packages=find_packages("pkg") and include_package_data=True in you setup() method. If not then I would recommend you to add it like this -

setup(
    ....
    package_dir={"": "pkg"},
    packages=find_packages("pkg"),
    include_package_data=True,
    ....
)
  • package_dir={"": "pkg"} - tells dsutil packages are under pkg

  • find_packages("pkg") - include all packages under pkg

  • include_package_data=True - include everything in source control

Hopefully with this you will be able to achieve what you want.

Upvotes: 1

Related Questions