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

Question:

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 everything: __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 also let me know if some other thing could be written more elegantly.

[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
Asked By: cutus_low

||

Answers:

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 an __init__ file like so:

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

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

from package import module1
Answered By: Davide Laghi

I had the similar issue, and I kept my setup.cfg like this –

[metadata]
name = my_proj
version = 0.0.1

[options]
python_version == "3.7.10"
package_dir=
    =src
packages=find:

[options.packages.find]
where=my_proj

[options.extras_require]
install_requires =
    boto3 == 1.15.3
    pyspark == 3.3.0
    smart-open == 6.3.0
test =
    pytest
    mock

[options.entry_points]
console_scripts =
    run_my_app = src.main.entry:fun1

My project name is my_proj and one of its subdirectory is src, which contains all the modules.

When you install the wheel generated by this setup, you can see what goes inside the site-packages of your venv is src along with its sub-directories.

Answered By: Ravindra