riverscuomo / spotkin_server

A python package that updates one or more of your Spotify playlists every day with a random selection of songs from any playlists you specify. Here's mine: https://open.spotify.com/playlist/1HaQfSGjNzIsiC5qOsCUcW?si=ddc16d3e9524410c . This repo also contains the backend server for Spotkin webapp.
GNU General Public License v3.0
83 stars 8 forks source link

Refactor import statements to use relative imports #24

Closed R055A closed 3 months ago

R055A commented 3 months ago

Fixes #23

Refactor import statements in the __main__.py file to use relative imports instead of a try-except block.

Before

try:
    from scripts.process_job import process_job
    from scripts.bans import *
    from scripts.post_description import *
    from scripts.api import *
    from scripts.utils import *
except:
    from spotkin.scripts.process_job import process_job
    from spotkin.scripts.bans import *
    from spotkin.scripts.post_description import *
    from spotkin.scripts.api import *
    from spotkin.scripts.utils import *

After

from .scripts.process_job import process_job
from .scripts.bans import *
from .scripts.post_description import *
from .scripts.api import *
from .scripts.utils import *
riverscuomo commented 3 months ago

hmm... this gave me the following error:

py spotkin                                       
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\RC Dropbox\Rivers Cuomo\Apps\spotkin\spotkin\__main__.py", line 6, in <module>     
    from .scripts.process_job import process_job
ImportError: attempted relative import with no known parent package

The try/except blocks are not pretty but they're working for my purposes.

R055A commented 3 months ago

hmm... this gave me the following error:

py spotkin                                       
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\RC Dropbox\Rivers Cuomo\Apps\spotkin\spotkin\__main__.py", line 6, in <module>     
    from .scripts.process_job import process_job
ImportError: attempted relative import with no known parent package

The try/except blocks are not pretty but they're working for my purposes.

Hi @riverscuomo

Thank you for the quick response.

Try py -m spotkin as instructed in the README.md. The -m flag is required for the relative imports.

The relative import changes worked for me when using the -m flag.

Testing passed when running the app as a 'standalone command line script' and when running the server with flask, seemingly resolving #23

I tested using an Anaconda environment with the commands python -m spotkin and flask run, but it shouldn't matter which environment is used so long that the -m flag is included with the python command.