ModuleNotFoundError: No module named 'metadata_setup' - python

I have a directory structure like the following:
/
/setup
/sqlalchemy
__init__.py
metadata_setup.py
/server
/data
__init__.py
simulations.py
In simulations.py I have:
import sys
sys.path.insert(0, '/setup/sqlalchemy')
import metadata_setup
but then I get the following error at the import statement:
ModuleNotFoundError: No module named 'metadata_setup'
I tried adding a __init__.py to the root directory but I'm still getting the same results. I'm running Python 3

Either insert full path or edit relative path like:
sys.path.insert(0, '../../../setup/sqlalchemy')

This worked for me:
import sys
sys.path.insert(0, os.path.join(os.path.dirname(sys.path[0]),'setup', 'sqlalchemy'))
import metadata_setup

Related

Importing a python file from another directory but in same parent

I have this strucutre:
app/src/main.py
app/test/test.py
I want to import a function from main.py to test.py
I tried adding __init__.py to the parent lib and also the directories but it gave me an error.
the error is:
ModuleNotFoundError: No module named 'app'
Try this:
import sys; sys.path.insert(1, os.path.join(sys.path[0], '..'))
sys.path.append('../src/')
import src.main as mainfn
mainfn.function()
change function() with the name of your function

How to import python module from a different directory into jupyter notebook

I have a project which is structured as below:
/project
/src
__init__.py
my_module.py
/notebooks
my_notebook.ipynb
I wanted to import my_module.py in my_notebook.ipynb, so I first tried import src.my_module getting ModuleNotFoundError: No module named 'src' and from ..src import my_module getting ImportError: attempted relative import with no known parent package. From several similar topics I found that adding /project location to SYSPATH would be a quick hack and it works indeed:
import sys
sys.path.insert(1, '/project')
from src import my_module
However, interfering with SYSPATH seems to be a nasty way and I wonder - does a safer/cleaner solution exists. Also, I don't quite get why from ..src import my_module doesn't work in the first place?

Import a package beyond top-level package using importlib

I am looking to import a package (plugin) outside my current directory. Say I have a directory structure:
/plugins
/test2
__init__.py
test_file.py
/src
main.py
The main.py has the following:
import importlib
import pkgutil
import os
import sys
module_dir = os.path.join(os.path.dirname(__file__) + "/../plugins")
for finder, name, ispkg in pkgutil.iter_modules([module_dir]):
print(finder, name, ispkg)
test = importlib.import_module(name, package='..plugins')
print(test.test_file.test_variable)
The test_file.py has the following:
test_variable = "testing"
With this current code, I would get an error of:
ModuleNotFoundError: No module named 'test2'
When I try to add sys.path.append(module_dir) so the path is included, I would get an error:
ValueError: attempted relative import beyond top-level package
What would be the best way to implement this and import the package/plugin? Assuming I could have multiple package under the plugin folder

import a module in current working directory

I have following directory structure,
/
__init__.py
__meta__.py
I tried to import __meta__ in a file but python complains about not finding a module named __meta__. I checked the current working directory for the file useing os.getcwd() which printed the directory with __meta__.py.
But I can import the module from python interpreter.
Append it to sys path first, and then try to import your module
import os
import sys
sys.path.append(os.getcwd())

ImportError when importing from another module

My app structure is the following:
./mod1/__init__.py
./mod1/utils.py
./mod2/__init__.py
./mod2/test.py
Now in ./mod2/test.py I do:
from mod1 import utils
But I get an ImportError that no module is named utils. What's wrong?!
In order for this to work, the parent directory of mod1 and mod2 must be in sys.path, which probably means that it needs to be in the environment variable PYTHONPATH. Please see the module search path documentation.
One solution that does not require modification of PYTHONPATH is to place your executable script in the parent directory of mod1 and mod2.
Add a top-level folder in the sys.path:
import sys
sys.path.append('path_to_app_folder')
You should write this line before from mod1 import utils. Also add __init__.py:
./mod1/__init__.py
./mod1/utils.py
./mod2/__init__.py
./mod2/test.py
__init__.py
You can dynamically get the path to the app folder from ./mod2/test.py, using os.path.abspath. So, you code for ./mod2/test.py will look like this:
import os
import sys
top_level_folder = os.path.abspath('../')
sys.path.append(top_level_folder)
from mod1 import utils

Categories