|Project
|--m1Folder
|--|--__init__.py
|--|--m1.py
|--m2Folder
|--|--__init__.py
|--|--m3Folder
|--|--|--__init__.py
|--|--|--m3.py
m1Folder and m2Folder are inside of Project. m3Folder is inside of m2Folder. All m*Folder contains empty __init__.py.
How to import m1.py, from m3.py file?
I tried from m1Folder import m1 as mo inside m3.py file and gave ModuleNotFoundError: No module named 'm1Folder'. However, pylint in vscode did not show any error.
from ...m1Folder import m1 as mo gives ValueError: attempted relative import beyond top-level package
You can do it by changing the system path with the sys module.
import sys
sys.path.insert(1, '/path/to/application/app/folder')
import m1
Related
File "C:\Users\sande\OneDrive\Desktop\two.py", line 8, in
import score.py
ModuleNotFoundError: No module named 'score.py'; 'score' is not a package
You can solve this issue by using this:
import score
If this score.py is not is not in same folder as your two.py then visit How to import the class within the same directory or sub directory to solve that issue.
Don't use import scope.py, if you are having problem importing try import scope (if both the files are in the same directory) or if the files are in different directories you can use the sys module in python
# importing sys
import sys
# adding Folder_2 to the system path
sys.path.insert(0, '/C:/Users/sande/OneDrive/{drive/folder}/scope.py')
# importing the functions/class
If you are still getting the error, try removing
if name == "main":
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
I have the following structure of my project
Project\
Core\
__init__.py (empty)
indicators.py
platform_core.py (which imports indicators)
Tests\
test.py (import Core here)
in test.py I have
import sys
sys.path.append(sys.path[0] + "/..")
import Core.platform_core
if I import Core.platform_core and run test.py, I get the following error
ModuleNotFoundError: No module named 'indicators'
This doesn't happen if I import Core. I also tried
import Core.indicators
import Core.platform_core
but get the same error.
My question is whether it is possible to do a relative import of a file that imports another file within the same sub module, without import the whole sub module?
Edit:
In platform_corer.py, the code that imports indicators is
from indicators import SMA
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())
In module MainClass.py which is in c:\MyProject\ModelClasses I'm
trying import module MainHelper.py from c:\MyProject\Helpers
from Helpers.MainHelper import MainHelper
x=MainHelper()
and I get error
ImportError: No module named Helpers.MainHelper
In WIng IDe I've added path c:\MyProject\Helpers Project->ProjectProperties\PythonPath
I'm using windows xp
If c:\MyProject\Helpers is in PYTHONPATH, you should write
from MainHelper import MainHelper
in the MainClass.py module.
If you want to treat the Helpers directory as a package, do the following:
PYTHONPATH should be c:\MyProject
The Helpers directory should have a (possibly empty) __init__.py file in it.
You would then use import statements like:
import Helpers.MainHelper
from Helpers.MainHelper import MainHelper
from Helpers import MainHelper
Documentation links:
http://docs.python.org/tutorial/modules.html
http://docs.python.org/tutorial/modules.html#packages