Python Forum
Setuptools CLI program ModuleNotFoundError after splitting up module into package
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setuptools CLI program ModuleNotFoundError after splitting up module into package
#1
Question 
Hi community!
I wrote a simple command line tool that basically copies Spotify tracks from one playlist to another (minus the duplicates).
For the application itself I used click and spotipy.
I used setuptools in order to be able to install the program and call it from the command line everywhere.
At first my program was in two files and the project structure looked roughly like that:

spotify-playlist-merger
│ configuration.py
│ setup.py
│ spotifypm.py

So the main code is in spotifypm.py while configuration.py is a Data Class used by spotifypm.py (It also should be named after CamelCase, right?).
If I then executed "pip3 install ." in the directory the program would install without any issues. After that I could access my program for example by entering "spotifypm --help" in the terminal.
The setup.py looks like this:
from setuptools import setup

setup(
    name='spotifypm',
    version='0.1.0',
    py_modules=['spotifypm', 'configuration'],
    install_requires=[
        'Click',
        'loguru',
        'spotipy',
        'PyYAML'
    ],
    entry_points={
        'console_scripts': [
            'spotifypm = spotifypm:cli',
        ],
    },
)
Because I thought that having all the logic in one big file was a little confusing, I decided to refactor my code. The structure ended up looking like this:

spotify-playlist-merger
│ setup.py
└─spotify_playlist_merger
.....│ __init__.py
.....│ Configuration.py
.....│ helpers.py
.....│ spotifypm.py


Executing the code via my IDE directly worked without issues but if I tried to install the program again via the setup.py and then executing "spotifypm --help" in the terminal returns the following error:

Traceback (most recent call last):
  File "C:\Users\Clemens\AppData\Local\Programs\Python\Python310\Scripts\spotifypm-script.py", line 33, in <module>
    sys.exit(load_entry_point('spotifypm==0.1.0', 'console_scripts', 'spotifypm')())
  File "C:\Users\Clemens\AppData\Local\Programs\Python\Python310\Scripts\spotifypm-script.py", line 25, in importlib_load_entry_point
    return next(matches).load()
  File "C:\Users\Clemens\AppData\Local\Programs\Python\Python310\lib\importlib\metadata\__init__.py", line 162, in load
    module = import_module(match.group('module'))
  File "C:\Users\Clemens\AppData\Local\Programs\Python\Python310\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 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\Clemens\AppData\Local\Programs\Python\Python310\lib\site-packages\spotify_playlist_merger\spotifypm.py", line 3, in <module>
    import Configuration
ModuleNotFoundError: No module named 'Configuration'
My setup.py looks like this:

import setuptools
from setuptools import setup

setup(
    name='spotifypm',
    version='0.1.0',
    packages=setuptools.find_packages(),
    include_package_data=True,
    install_requires=[
        'Click',
        'loguru',
        'spotipy',
        'PyYAML'
    ],
    entry_points={
        'console_scripts': [
            'spotifypm = spotify_playlist_merger.spotifypm:cli',
        ],
    },
)
Can anyone help me out here? I tried to replicate what they did in the documentation of click: https://click.palletsprojects.com/en/8.0.x/setuptools/
For a closer look, the project is on gitlab as well: https://gitlab.com/clemwo/spotify-playli...ree/master
Reply
#2
I solved the issue by creating a subpackage called "scripts" where I placed the spotifypm.py file and left the other files in the package above that. My setup.py now looks like this:

import setuptools
from setuptools import setup

setup(
    name='spotifypm',
    version='0.1.0',
    packages=setuptools.find_packages(),
    include_package_data=True,
    install_requires=[
        'Click',
        'loguru',
        'spotipy',
        'PyYAML'
    ],
    entry_points={
        'console_scripts': [
            'spotifypm = spotify_playlist_merger.scripts.spotifypm:cli',
        ],
    },
)
The structure is as follows:

spotify-playlist-merger
│ setup.py
└─spotify_playlist_merger
.....│ __init__.py
.....│ Configuration.py
.....│ helpers.py
.....└─scripts
..........│ __init__.py
..........│ spotifypm.py

I'd still be interested why the entry point apparently has to reside in an extra package? Why didnt it work before?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error: "ModuleNotFoundError: No module named 'RPi'" LucaCodes 4 505 Dec-29-2021, 01:58 PM
Last Post: LucaCodes
  "ModuleNotFoundError: No module named 'PyQt5.QtWidgets'; 'PyQt5' is not a package" chipx 3 371 Dec-09-2021, 07:05 AM
Last Post: chipx
  ModuleNotFoundError: No module named 'Seal' Anldra12 10 1,339 Oct-06-2021, 12:12 PM
Last Post: snippsat
  ModuleNotFoundError: No module named 'bluetooth' Error Rovelin 4 1,585 Aug-31-2021, 04:04 PM
Last Post: Rovelin
  Scipy sparse setuptools issues Anldra12 11 1,534 Jun-19-2021, 04:01 PM
Last Post: Anldra12
  ModuleNotFoundError: No module named 'graph' Anldra12 3 2,843 May-02-2021, 02:41 PM
Last Post: Anldra12
  ModuleNotFoundError: No module named 'fpdf' KatMac 4 3,243 Apr-19-2021, 01:23 PM
Last Post: KatMac
  ModuleNotFoundError: No module named 'com' aleksg 9 3,889 Dec-14-2020, 05:00 PM
Last Post: Larz60+
Video ModuleNotFoundError: No module named ‘…’ Frank911 1 2,222 Dec-11-2020, 03:59 PM
Last Post: michael1789
  ModuleNotFoundError: No module named 'fcntl' ErnestTBass 2 8,873 Dec-05-2020, 02:39 PM
Last Post: ErnestTBass

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020