0

I have two python files: main.py and imported.py. I want to import imported.py into main.py but I want access to the path of main.py in the file imported.py. That is I want access to the path of the importing module. For example:

#main.py
import imp
imported = imp.load_source('imported', "/absolute/path/to/imported.py")

#imported.py
pathToImportingModule=os.path.??? 
doSomethingWithPath(pathToImportingModule)
user1763510
  • 1,070
  • 1
  • 15
  • 28
  • This question seems to be similar, with no answer: https://stackoverflow.com/questions/43081310/getting-list-functions-in-file-that-imported-a-module – user1763510 Mar 03 '18 at 23:47
  • This looks like an X-Y problem. What are you actually trying to do? – zvone Mar 04 '18 at 00:09
  • imported.py is a general script that I will run a lot. This script will do tasks related to the folder that main.py is in. For example, it will create a new folder in the same folder that main.py is in, and create files in that folder. I know I can simply get the path of folder that conatins main.py in main.py using os.path.dirname(os.path.abspath(os.path.realpath(__file__))) and then pass that to imported.py, but I wondering if there is a way to do this automatically. – user1763510 Mar 04 '18 at 00:20
  • 1
    @user1763510. How about: `os.path.abspath(sys.modules['__main__'].__file__)`? – ekhumoro Mar 04 '18 at 00:58
  • Exactly what I was looking for ekhumoro – user1763510 Mar 04 '18 at 01:12
  • 1
    @user1763510 I'm glad you found your solution. I hope you don't mind if I offer an alternative: put main.py together with imported.py, don't use `imp.load_source`, don't do things *"in the same folder that main.py is in"*, do things in current working directory instead, or take the work directory as an argument. That way you will have a much more logical setup which will be easier to maintain. – zvone Mar 04 '18 at 16:31
  • @zvone I agree that this isn't the most logical and clean setup. However, I am trying to share this imported.py file with other individuals that will create the main.py file on their own. These other users may not be so competent with python so I want to make this as simple and minimal as possible to avoid confusion. – user1763510 Mar 04 '18 at 17:21

2 Answers2

0

I don't know any "importing" notification hook. You can achieve some what similar thing by explicitly calling a function into imported module:

#main.py
import imp
import sys
import os

imported = imp.load_source('imported', "/absolute/path/to/imported.py")
imported.doSomething(os.path.abspath(sys.modules[__name__].__file__))

#imported.py
def doSomething(importing_path):
   print ("Importing path: ", importing_path)
   pass
jbp
  • 1,581
  • 1
  • 10
  • 15
0

Solution received in comments from ekhumoro:

#main.py
import imp
imported = imp.load_source('imported', "/absolute/path/to/imported.py")


#imported.py
pathToImportingModule=os.path.abspath(sys.modules['__main__'].__file__)    
doSomethingWithPath(pathToImportingModule)
user1763510
  • 1,070
  • 1
  • 15
  • 28