relative import with no known parent package - python

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")

Related

Import Error: attempted relative import with no known parent package

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 need help importing modules into tests from sibling folder

I get an import error "unable to import module" when I try to import a module
File structure
src
modules
__init__.py
authenticate_developer
twttier_importer
notebooks
main.ipynb
unit_tests
test_authenticate_developer
In test_authenticate_developer
import requests
from nose.tools import assert_true
import os
import sys
sys.path.append(os.path.abspath('../modules'))
import settings
import twitter_importer #returns import error
import authenticate_developer #returns import error
However when I use the same syntax in my notebooks it is successful.
import sys
import os
sys.path.append(os.path.abspath('../modules'))
sys.path.append(os.path.abspath('../'))
import twitter_importer
import authenticate_developer
import settings
I have looked at existing answers and I tried them out i.e., removing init.py from the root folder and removing or adding init.py to the tests folder. None seems to work for me.
I managed to get someone to help me with these and this is how we tackled it.
src
modules
__init__.py
authenticate_developer
twttier_importer
notebooks
main.ipynb
unit_tests
__init__.py
test_authenticate_developer
In test_authenticate_developer
import os,sys,inspect
sys.path.append(os.path.abspath('../modules'))
#This is to access the parent folder in order to import config
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
import config
from modules.authenticate_developer import Authenticate
I cannot explain why I have to do it differently in the test files but this is my work around

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())

Can't import a module in parent directory on App Engine

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!

Categories