1

I have a script myscript.py and within it I have some relative import:

# here the relative import fails if I didn't launch the application through
# the script itself or if I am not executing the whole script.
prodquery=open('.\ProductionQueries\myquery.sql','r')
myquery=prodquery.read()
prodquery.close()

I often have the need to run specific cells of the script and I need to stick to relative imports as I work on different machines.

I found plenty of similar questions but each answer refers to the case of executing the entire script (e.g. this or this).

Is there a way to retrieve the script path and update the current directory accordingly? I would assume that the information is stored somewhere, since it appears in the editor. example

Can anyone point me in the right direction? (or maybe to the right answer if this is already on SO.)

CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56

0 Answers0