Thursday, March 31, 2022

[SOLVED] Issue with find in setup.cfg | modules are not seen in path

Issue

I've been trying to properly package my python codes, upload it on git, and install in on an virtual environment all of which I did and seem to work just fine. The issue is that once I pip install my code on the virtual environment, when I run my package as:

python3 -m mypackage

it raises an error suggesting none of the modules that I imported in my _main_.py (my package has one single directory that contains eveything: _init_.py , _main_.py , module1.py , etc. ) are seen. However, once I "cd" into where the package is installed in the virtual environment, it can see all the modules and everything works. My guess is there's an issue with my setup.cfg file and I guess the "find" commands are not doing what they are supposed to do (please let me know if some other thing could be more efficiently written; I'm a beginner):

[metadata]
# replace with your username:
name = my_package
version = 0.0.1
author = my_name
author_email = [email protected]
description = 
long_description = file: README.md
long_description_content_type = text/markdown
url = https://github.com/myname/mypackage
project_urls =
    Bug Tracker = https://github.com/myname/mypackage/issues
classifiers =
    Programming Language :: Python :: 3
    License :: OSI Approved :: MIT License
    Operating System :: OS Independent

[options]
package_dir =
    = src
packages = find:
python_requires = >=3.6
install_requires =
    numpy > 1.0 
    opencv-python >= 4.0

[options.packages.find]
where = src

Solution

i had a similar issue and it troubled me for some days. In the end I understood that the problem was in the folder structure. If you use the find: function in setup.cfg then it will look for packages under /src but if you have only modules (like I had) it will not find anything.

You should should put your modules inside a folder with a init file like so:

src
 |
 |-----package:
          |
          |------ __init__.py
          |------ module1.py
          |------ module2.py
          |------ ...

Note that when you will try to import module1 for instance you will need to import with:

    from package import module1


Answered By - Davide Laghi
Answer Checked By - Dawn Plyler (WPSolving Volunteer)