简体   繁体   中英

Figure out the module name trying to use imports in one module python

I am trying to figure out if there is a way to detect the name of the module that is trying to import things from a module, for example I have a module called "all_modules" where I store all the imports I would need across the folder and thought it would be easier to import that in teh files then multiple of the same import across all my files but by doing this I noticed it stops trying to import modules once it comes across importing itself. For example this is what my general idea of doing this is:

   #all_modules.py
   import sys, os, time, threading, re, random, urllib.request, urllib.parse
   import a, b, c, d # fake modules but general idea is here
   # any modules you would need throughout the folder really this is just a small example

   #a.py 
   from all_modules import * 
   print(sys.version_info) # works fine but lets do the last module

   #c.py
   from all_modules import *
   and_one_more.someFunction("Hello, World!") # it didn't import it because it stopped when it tried to import itself

So my idea is figure out the name of file that is trying to access the imports then do this

  #all_modules.py - Example two
  module_list = ["sys", "os", "time", "threading", "re", "random", "urllib", "urllib.request", "urllib.parse", "a", "b", "c"]
  for module in module_list:
      if file_name == module: continue # this is what I do not know how to do and not sure if it is even possible, but it's the only solution I have to be able to try to do what I am doing 

      globals()[module] = __import__(module)

I'm wondering if there is some work around to stop this, or will I have to import all the modules I use throughout the file in each one? As it stands I get errors of another module not being imported because it didn't import the rest after it came across itself, so I was wondering if what I'm trying to do is possible or something that fixes the problem would be great, or will I have to individually import the modules in throughout the files?

How about using load_module method from imp module:

#!/usr/bin/python

import imp
import sys

def __import__(name, globals=None, locals=None, fromlist=None):

    try:
        return sys.modules[name]
    except KeyError:
        pass

    fp, pathname, description = imp.find_module(name)
    try:
        return imp.load_module(name, fp, pathname, description)
    finally:
        if fp:
            fp.close()

module_list = ["sys", "os",
               "time", "threading",
               "re",
               "urllib" ]

for module in module_list:
    module = __import__(module)
    print module

Output:

 <module 'sys' (built-in)>
 <module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
 <module 'time' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/time.so'>
 <module 'threading' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.pyc'>
 <module 're' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.pyc'>
 <module 'urllib' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.pyc'>

The problem you're facing is the classic cyclical import problem. In python, the easiest way to work around it is to simply not to do what you are doing, and if you absolutely have 2 modules that depend on each other, they you can simply import the module as a namespace and not everything defined inside it (ie. import x vs from x import * ).

In general, * imports are frowned upon in python because it's not clear to someone else reading your code where things are defined. It's basically defeating the purpose of namespaces.

You should really read PEP8 . It's a style guide that you should try to follow if you ever intend on releasing your code out into the world.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM