0

Sorry for the really bad title this is hard to explain but im making a python script that can play songs in spotify and i imported 3 variables from another file called secrets.py and only two of the variables from secrets are working

here's the code from main.py:

import requests
from secrets import spotify_user_id, spotify_token, damn_id

class SaveSongs:
    def __init__(self):
        self.user_id = spotify_user_id
        self.spotify_token = spotify_token
        self.damn_weekly_id = damn_id


    def find_songs(self):

        #loop through playlist tracks

        query = "https://api.spotify.com/v1/playlists/{}/tracks" .format(
            damn_id)

        response = requests.get(query,
                                headers={"Content-Type": "application/json",
                                        "Authorization": "Bearer {}".format(spotify_token)})

        response.json = response.json()
        print(response)

a = SaveSongs()
a.find_songs()

Here's the code from secrets

Code:

spotify_user_id = "..."

spotify_token = "..."

damn_id = "5V9DHomv3ymanLUnPCRdD4"

I put periods in the token and userid because I can't show that information

And also here's the error I get when executing the script

Traceback (most recent call last):
  File "c:/Users/Documents/Python spotify/main.py", line 3, in <module>
    from secrets import spotify_user_id, spotify_token, damn_id
ImportError: cannot import name 'spotify_user_id' from 'secrets' (c:\Users\Documents\Python spotify\secrets.py)
Chexz
  • 19
  • 5
  • 1
    Try to rename your module. There's built-in [`secrets`](https://docs.python.org/3/library/secrets.html). Probably you can't import any of those variables, because python search them in module I referenced and none of them exists there. – Olvin Roght Aug 11 '21 at 10:59
  • both the files are in same directory? – Muhammad Adeel Aug 11 '21 at 11:01
  • @OlvinRoght, however, the error message complains about the _local_ file `'secrets' (c:\Users\Documents\Python spotify\secrets.py)`, not the built-in module – ForceBru Aug 11 '21 at 11:01
  • Yes they are both in the same directory – Chexz Aug 11 '21 at 11:02
  • I don't know what a built-in module is im really confused – Chexz Aug 11 '21 at 11:02
  • 1
    @OlvinRoght that's not the issue here. [The modules in the root folder have precedence over the PYTHONPATH modules when it comes to importing.](https://stackoverflow.com/a/47531592/4349415) – Mike Scotty Aug 11 '21 at 11:02
  • @ForceBru, that's the only more or less logical reason I found. – Olvin Roght Aug 11 '21 at 11:03
  • @ForceBru so what can I do? – Chexz Aug 11 '21 at 11:04
  • 1
    Can't reproduce. Make sure you: 1) saved `secrets.py`; 2) are running Python from the directory where `secrets.py` is located (`c:\Users\Documents\Python spotify`) – ForceBru Aug 11 '21 at 11:04
  • 1
    @Chexz, also try delete line with `spotify_user_id` assignment and reprint it again, maybe there's some unicode whitespace which recognized as part of variable name. – Olvin Roght Aug 11 '21 at 11:06
  • oh my god im so stupid... I just saved it and it worked LOL i thought it auto saves thanks bro – Chexz Aug 11 '21 at 11:06

0 Answers0