0

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=[] ) 
4
  • 1
    can you include the content of your setup.py in your post? Commented Jun 18, 2021 at 12:08
  • 1
    and just to be sure, you did run pip install -e . before trying to import, right? Commented Jun 18, 2021 at 12:09
  • used pip3 install pkg.whl @Arne Commented Jun 18, 2021 at 12:13
  • 1
    these things can be tricky to debug, so I won't write an answer yet. can you first try to 1) add an empty __init__.py file directly under pkg, and 2) import find_packages as well, and in your setup call update packages=find_packages("pkg") Commented Jun 18, 2021 at 12:20

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.