After saving my main.py file for FastAPI web server, I have received this error:
from . import models
ImportError: attempted relative import with no known parent package
Inside main.py, I was trying to import models.py file to main.py. Both files are under same directory:
from . import models
Related
Hello i have a difficulty in understanding this concept of package and don't know why i am getting this error
i have a structure like this
./unittests:
rfcg.py
./tests:
test_misc.py
__init__.py
the unitests is the top folder containing rcfg module and subfolder tests that contains test_misc.py
in test_misc.py when i try to import
from ..rcfg import some_class
it has been auto completed by vscodem, but when i print it is giving me error, the error is
print(run_cfg) -----> run_cfg is some_class
from ..rcfg import run_cfg
ImportError: attempted relative import with no known parent package
i have also created a init.py in subfolder tests also but still it is the same
I'm going to go ahead and ask question # 2,345,651 about file imports in python. But I've been on SO for the last hour and have just sunk two hours in the drain trying to port a function from one directory into another.
My situation:
I have the following application with the current file structure:
trading/
-- app/
models.py
-- db/
engine.py
trading, app, and db all have an __init__.py file inside of them.
At first I tried different combinations of this:
from trading.db.engine import load_engine
from db.engine import load_engine
from .db.engine import load_engine
from ..db.engine import load_engine
I get the following error messages, respectively:
ModuleNotFoundError: No module named 'trading'
ModuleNotFoundError: No module named 'db'
ImportError: attempted relative import with no known parent package
ImportError: attempted relative import with no known parent package
I also know there is the PYTHONPATH value that you can set, and I've done that too.
Here are screenshots of my current environment variables in Windows System Manager:
For my entire system:
For my own username:
But yet I cannot use the load_engine function from the engine.py in the db folder for the models.py file in the app folder.
Genuinely looking for help. Thank you.
I'm trying to import an class into my test (test_account.py), but I get the following error:
from ..src.account_status import TestClass
ImportError: attempted relative import with no known parent package
This is my directory I have scr and test in the same App directory.
./App/src/account_status.py
./App/test/test_account.py
My import in test_account.py:
from ..src.account_status import TestClass
I have tried adding __init__.py to both folders and the App directory, but I get the same message, why?
Try this
import sys
sys.path.append("../src")
from account_status import TestClass
sys.path.remove("../src")
I have created init.py file on all my folders which should be essentially packaged the folder as a package.
I keep getting a 'No module named 'xxx.xxx' message.
The IntelliSense on visual studio however shows the module.
I have tried adding
1. import line on the 'init' file on the folders
2. import line on the main file from which i am trying to access the module
3. 'from x import y' on the mainfile
import os
import ml
import ml.controller
from ml import controller
Project setup on visual studio-
ProjectName
--ml(folder)
-__init__.py
-controller(folder)
-__init__.py
-classification(folder)
-__init__.py
-k-means.py
runserver.py
I am trying to access the k-means.py class from runserver.py.
I except to import the module on my main file.
Say I have a module called "test_module", but I want to import a file within the parent directory with this kind of folder structure:
/app_root
__init__.py
script_to_import.py
/test_module
test_script.py
Basically I want to be able to import "script_to_import.py" from "test_script.py". I added the following code to test_script.py in order to add the parent directory:
import sys
import os
sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
However, I'm still getting a "ImportError: No module named script_to_import" error when I try to run dev_appserver.py. Any ideas how to add a parent directory to my python path for import?
Thanks!