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
Related
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
Here's my folder structure:
src
->deployment_pipeline
->__init__.py, train_pipeline.py
src
->dags
->__init__.py,airflow_dag.py
src
->db_connector_mlflow
-> __init__.py, db_connector_mlflow.py
Now, I am trying to import a function start_final_train from train_pipeline.py(which is inside the folder deployment_pipeline) to airflow_dag.py and from db_connector_mlflow.py(which is inside the folder db_connector_mlflow) to airflow_dag.py
My import statement:
from deployment_pipeline import start_final_train
But I keep getting this error:
ModuleNotFoundError: No module named 'deployment_pipeline'
imports must be globally installed or be in the same directory or subdirectory thereof as your main file.
If you move your main file to the src folder and run everything from there it works out.
Your main file should import:
from dags.airflow_dag import <stuff you need from airflow_dag.py>
....
You should keep the same structure in airflow_dag.py to import your function (as if you were importing from src):
from deployment_pipeline.train_pipeline import start_final_train
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
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 *
I need to dynamically import modules into my project from another package.
The structure is like:
project_folder/
project/
__init__.py
__main__.py
plugins/
__init__.py
plugin1/
__init__.py
...
plugin2/
__init__.py
...
I made this function to load a module:
import os
from importlib.util import spec_from_file_location, module_from_spec
def load_module(path, name=""):
""" loads a module by path """
try:
name = name if name != "" else path.split(os.sep)[-1] # take the module name by default
spec = spec_from_file_location(name, os.path.join(path, "__init__.py"))
plugin_module = module_from_spec(spec)
spec.loader.exec_module(plugin_module)
return plugin_module
except Exception as e:
print("failed to load module", path, "-->", e)
It works, unless the module uses relative imports:
failed to load module /path/to/plugins/plugin1 --> Parent module 'plugin1' not loaded, cannot perform relative import
What am I doing wrong?
I managed to solve my own issue after a LOT of googling. Turns out I needed to import using relative paths:
>>> from importlib import import_module
>>> config = import_module("plugins.config")
>>> config
<module 'plugins.config' from '/path/to/plugins/config/__init__.py'>
>>>
I had a similar problem not long ago. I added the path of the project folder to the sys.path using the module's absolute path like this:
import sys
import os
sys.path.append(os.path.dirname(os.path.realpath(__file__))+'/..')
This adds the project_folder to the sys.path thus allowing the import statement to find the plugin modules.