how to import all files from different folder in Python - python

with __init__.py in the directory, I was able to import it by
from subdirectory.file import *
But I wish to import every file in that subdirectory; so I tried
from subdirectory.* import *
which did not work. Any suggestions?

Found this method after trying some different solutions (if you have a folder named 'folder' in adjacent dir):
for entry in os.scandir('folder'):
if entry.is_file():
string = f'from folder import {entry.name}'[:-3]
exec (string)

If you have the following structure:
$ tree subdirectory/
subdirectory/
├── file1.py
├── file2.py
└── file3.py
and you want a program to automatically pick up every module which is located in this subdirectory and process it in a certain way you could achieve it as follows:
import glob
# Get file paths of all modules.
modules = glob.glob('subdirectory/*.py')
# Dynamically load those modules here.
For how to dynamically load a module see this question.
In your subdirectory/__init__.py you can import all local modules via:
from . import file1
from . import file2
# And so on.
You can import the content of local modules via
from .file1 import *
# And so on.
Then you can import those modules (or the contents) via
from subdirectory import *
With the attribute __all__ in __init__.py you can control what exactly will be imported during a from ... import * statement. So if you don't want file2.py to be imported for example you could do:
__all__ = ['file1', 'file3', ...]
You can access those modules via
import subdirectory
from subdirectory import *
for name in subdirectory.__all__:
module = locals()[name]

Your __init__.py file should look like this:
from file1 import *
from file2 import *
And then you can do:
from subdirectory import *

Related

How can I import module from another folder which is not same directory structure

I have two directory on same level
1.Scripts->(Inside it we have app.py file)
2. Keymaker-->(Inside it we have keymaker.py file)
Now I want to import keymaker.py in app.py, So how Can I do it
Write these lines in app.py file.
import sys
sys.path.append('path/to/Keymaker/dir')
After that you can import anything from keymaker.py file using
from keymaker import *
Add empty __init__.py file to Keymaker directory and do from Keymaker import keymaker

How to import module from one directory to another?

I am trying to import python module from one directory to another directory but not able to do it.
Here is my folder structure
Parent_Folder
---------Module1
----__init__.py (empty file)
----mymodule1.py
---------Module2
----__init__.py (empty file)
----mymodule2.py
I am trying to import
mymodule1.py
into
mymodule2.py
Here is the command that I have used to import the python module:
import Module1.mymodule1
but getting an error like
ModuleNotFoundError: No module named 'Module1'
I do see there are option like system-path at runtime, PYTHONPATH but I don't want to use those options.
Do you all have any other recommended solutions?
You can insert this at the top of module1.py, or in the init.py in Module1 if you're importing module1.py as well:
import sys
sys.path.append("../Module2") #assuming linuxOS
import module2
This adds the Module2 directory to the system path and allows you to import files as if they were local to your current directory.
Other answers can be found in this link: beyond top level package error in relative import
I'll give you an example of importing modules
I created the module1.py file
print("Module : 1")
def module01():#function
print("module : 1.2")
class Module02:# class
def module021(self):# function
print('MODULE 1')
to import you need to indicate the file name
module2.py
from module1 import module01 #import the function
from module1 import Module02 #importing the class from module2.py
module01()
mod1 = Module02() #
mod1.module021()# imported class function

How to import a file from a subfolder when you are in another subfolder? (python)

I'm currently in the 'strategy.py' file and I'm trying to import 'utils.py' and 'BaseStrategy.py' ('utils.py' is a file with some functions in it, 'BaseStrategy.py' contains a class with the name 'BaseStrategy').
Folder structure:
program\
__init__.py
main.py
folder1\
__init__.py
utils.py
BaseStrategy.py
folder2\
__init__.py
strategy.py
In 'main.py' I'm able to import both files like this:
from folder1.BaseStrategy import BaseStrategy
from folder1 import utils
So how can I import 'utils.py' and 'BaseStrategy.py' in 'strategy.py'?
import sys
sys.path.insert(0, 'program/folder1')
from folder1 import utils
from folder1 import BaseStrategy
You can also change the:
sys.path.insert(0, 'program/folder1')
To:
sys.path.append('../')
But it will mess up with the import from your parent directory (main.py). However you can overcome that with :
import os,sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
Essentially, you need to put a blank file named __init__.py in the subdirectory. If you want to take a look at the documentation you can do so here (6.4).
find:
./main/
./main/folder2
./main/folder2/__init__.py
./main/folder1
./main/folder1/base.py
./main/folder1/__init__.py
./main/__init__.py
cat ./main/folder1/base.py
import sys
import os
sys.path.append(os.getcwd() + '/..')
import folder2

Python Importing files from other folders (within the same project) using __init__.py?

Trying to import aFile.py from within the bSubFile.py but getting an error saying 'exceptions.ValueError, Attempted relative import in non-package'
My file structure is as follows:
app/
- __init__.py
FolderA/
- __init__.py
- aFile.py
FolderB/
- __init__.py
- bFile.py
SubfolderB/
- __init__.py
- bSubFile.py
I am trying to import aFile from bSubFile.py
tried:
from ..FolderA import aFile
class bSubFile():
...
and:
from ...FolderA import aFile
class bSubFile():
...
but I always get 'Attempted relative import in non-package', I must be missing something very obvious. Thanks!
You can add the other path to your system path. This may be not the most elegant way, but it works.
import sys
import os
# get the folder of the current file, go one directory back and add "FolderA"
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "FolderA"))
# now you can import everything that is in FolderA directly
import aFile

Loading all modules recursivly from subfolders in Python

I have following file structure
./AppFolder
__init__.py
main.py
./plugin
__init__.py
./simple_plugin_1
__init__.py
simple_plugin_1.py
./simple_plugin_2
__init__.py
simple_plugin_2.py
and i want to load all plugin modules recursively into main.py. So can I use __init__.py in AppFolder as following?
import os
import glob
__all__ = [ os.path.basename(f)[:-3] for f in glob.glob(os.path.dirname(__file__)+"/plugin/*/*.py")]
Or is there are other techniques?
I have this bit of code that I use for this purpose, just use this as your top level __init__.py:
import pkgutil
__all__ = []
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
module = loader.find_module(module_name).load_module(module_name)
exec('%s = module' % module_name)
__all__.append(module_name)
Basically same idea as what you have, but just using pkgutil.walk_packages to find all the modules instead of a file glob.

Categories