Issue
I have a variable in a python file named secrets.py
that I am trying to access from within IPython (specifically, an API key). If I try from secrets import API_KEY
, I get an error that it cannot import the variable. What's interesting, however, is that a standard python environment (i.e. running python
in a terminal) can import the variable just fine. Both the standard python environment and the IPython are run from the same directory/virtual environment. I'm not sure what's going wrong here. Any help would be appreciated.
Solution
IPython assumed that the secrets.py
that I was importing was Python's native secrets.py
and not the one I had made. Rather than mess with path variables or whatever else would be needed to work around this, simply renaming my secrets.py
to something else fixed the problem. I'm still not sure why this behavior is different in a normal python environment, so if anyone can explain that, I'm all ears.
EDIT: With some guidance, I figured out why they were behaving differently. Even though IPython's sys.path
has the current working directory in it, it was after the folder where python's secrets.py
existed. So IPython checked that folder, saw that there was a secrets.py
, and assumed that that was the one it was meant to import the API key from. Whereas with the normal python, the CWD was the first entry in the list, so it found the correct API key no problem.
Answered By - Catyre
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.