Search code examples
pythonworking-directory

Do imported modules have the same working directory as the file being executed?


Suppose I have a file called myfile.py in /Users/joe/Documents:

import mymodule

mymodule.foobar()

Now let's say that I need to fetch the current working directory of myfile.py in mymodule (which is in another location). Do they both have the same working directory because I am importing mymodule into myfile, or does mymodule have it's working directory as the directory of where it was installed


Solution

  • Because you are importing the module, they both have the same working directory, meaning that completing operations with os will be successful (or whatever other purpose you are using the current working directory for).