简体   繁体   中英

Importing multiple files named sequentially in Python

I have about 50 Python files in a directory, all named in a sequential order, for example: myfile1 , myfile2 ..... myfile50 . Now, I want to import the contents of these files inside another Python file. This is what I tried:

i = 0
while i < 51:
    file_name = 'myfile' + i
    import file_name
    i += 1

However, I get the following error:

ImportError: No module named file_name

How may I import all these sequentially named files inside another Python file, without having to write import for each file individually?

You can't use import to import modules from a string containing their name. You could, however, use importlib :

import importlib

i = 0
while i < 51:
    file_name = 'myfile' + str(i)
    importlib.import_module(file_name)
    i += 1

Also, note that the "pythonic" way of iterating a set number of times would be to use a for loop:

for i in range(0, 51):
    file_name = 'myfile' + str(i)
    importlib.import_module(file_name)

The other answer by @Mureinik is good, but simply doing - importlib.import_module(file_name) is not enough. As given in the documentation of importlib -

importlib.import_module(name, package=None)

Import a module. The name argument specifies what module to import in absolute or relative terms (eg either pkg.mod or ..mod). If the name is specified in relative terms, then the package argument must be specified to the package which is to act as the anchor for resolving the package name (eg import_module('..mod', 'pkg.subpkg') will import pkg.mod). The specified module will be inserted into sys.modules and returned.

importlib.import_module simply returns the module object, it does not insert it into the globals namespace, so even if you import the module this way, you cannot later on use that module directly as filename1.<something> (or so).

Example -

>>> import importlib
>>> importlib.import_module('a')
<module 'a' from '/path/to/a.py'>
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

To be able to use it by specifying the name, you would need to add the returned module into the globals() dictionary (which is the dictionary for the global namespace). Example -

gbl = globals()
for i in range(0, 51):
    file_name = 'myfile{}'.format(i)
    try:
        gbl[file_name] = importlib.import_module(file_name)
    except ImportError:
        pass #Or handle the error when `file_name` module does not exist.

It might be also be better to except ImportError incase file_name modules don't exist, you can handle them anyway you want.

@Murenik and @Anand S Kumar already gave a right answers, but I just want to help a little bit too :) If you want to import all files from some folder, it's better to use glob function instead hardcoded for cycle. It's pythonic way to iterate across files.

# It's code from Anand S Kumar's solution
gbl = globals()
def force_import(module_name):
    try:
        gbl[module_name] = importlib.import_module(module_name)
    except ImportError:
        pass #Or handle the error when `module_name` module does not exist.

# Pythonic way to iterate files
import glob
for module_name in glob.glob("myfile*"):
    force_import( module_name.replace(".py", "") )

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