I have seen several questions related to this error but I think they are different cases. My Django project has 2 applications and a directory structure like this and I'm facing an issue with relative imports I don't understand the logic.
Python allows me to import market from file my_app2/signals.py but it returns ValueError: attempted relative import beyond top-level package if I import portfolio from file my_app1/signals.py. What is the reason and how can I found a wordaround?
/my_project
/my_project
__init__.py
/my_app1
market.py
signals.py # Here I can't import portfolio.py
/my_app2
portfolio.py
signals.py # Here I can import market.py
my_app1/signals.py
from ..my_app2 import portfolio # doesn't work
ValueError: attempted relative import beyond top-level package
from my_project.my_app2 import portfolio # doesn't work
ModuleNotFoundError: No module named 'my_project.my_app2'
my_app2/signals.py
from ..my_app1 import market # works
I finally solved the issue without the use of from, it's not clear for me what was causing the error.
import my_app2.portfolio as portfolio
Related
I am trying to import some functions and variables from the parent directory.
Here is what my dir looks like:
I want the import:
-- send_invoice func from bifatura_methods.py
-- start func from xml_generator.py
-- data_json_1, data_json_2 dicts from inputs.py
TO send_ticari_satis_invoice.py.
My working dir is:
/home/selman/PycharmProjects/15.0/custom_addons/Invoice_Integration/models/tests
When I tried for imports like:
from ..bifatura_methods import send_invoice
from ..xml_generator import start
from inputs import data_json_1, data_json_2
Output:
ImportError: attempted relative import with no known parent package
When I tried this:
from custom_addons.Invoice_Integration.models.bifatura_methods import send_invoice
from custom_addons.Invoice_Integration.models.xml_generator import start
from custom_addons.Invoice_Integration.models.tests.inputs import data_json_1,data_json_2
Output:
ImportError: cannot import name 'models' from 'odoo' (unknown location)
Any advice and help are welcome! Thank you community :)
since your \tests is essentially a module you're importing into, you should add an empty __init__.py inside \tests.
I personally would use explicit imports:
from models.bifatura_methods import send_invoice
from models.xml_generator import start
IMO, this would help you keep your sanity if you end up having a lot more submodules.
You can check the Odoo guidelines that we use from odoo.addons to import from Odoo addons (custom addons should also work)
Example:
from odoo.addons.Invoice_Integration.bifatura_methods import send_invoice
Based on some answers I try to be more specific.
I want to import the print and the models AND code in my main.py
I know the question gets asked a lot, but still I could not figure out whats wrong with my code!
I have a project directory like this
-project
--__init__py
--main.py
--print.py
--requests
--__init__.py
--models.py
--code.py
i want to import from print.py and * from requests
Therefore I tried to add these lines in main.py
from . import print
#or
import print
#for requests I tried
import os.path
import sys
sys.path.append('./requests')
from requests import *
all of those lines cause the same ImportError attempted relative import with no known parent ,
using Python 39
anyone an idea where the problem is?
I am very confused that this seems not to work, was it possible in older versions?
You should definitely not be doing anything with sys.path. If you are using a correct Python package structure, the import system should handle everything like this.
From the directory structure you described, project would be the name of your package. So when using your package in some external code you would do
import package
or to use a submodule/subpackage
import project.print
import project.requests
and so on.
For modules inside the package you can use relative imports. When you write
i want to import from print.py and * from requests Therefore I tried
it's not clear from where you want to import them, because this is important for relative imports.
For example, in project/main.py to import the print module you could use:
from . import print
But if it's from project/requests/code.py you would use
from .. import print
As an aside, "print" is probably not a good name for a module, since if you import the print module it will shadow the print() built-in function.
Your main file should be outside the 'project'-directory to use that as a package.
Then, from your main file, you can import using from project.print import ....
Within the project-package, relative imports are possible.
I have two django applications in my project in my project named electron: the first api and the second elec_meter. The directories are organized as follows:
electron /
api/
views.py
...
elec_meter/
models.py
...
In the api / views.py file I want to import elec_meter / models.py.This is how I do it:
from ..elec_meter.models import *
But I get the following error message:
ValueError: attempted relative import beyond top-level package
or
from electron.elec_meter.models import *
In this case I receive this error message:
ModuleNotFoundError: No module named 'electron.elec_meter'
Here is a picture of my code
How can I solve this problem?
The Python root path is the electron directory, so you can not work with from electron.….
You can import the objects by importing it starting with the name of the app, not the project. This thus means that you import this with:
from elec_meter.models import Model1, Model2
while you can make wildcard imports, it is often considered an antipattern, since it is unclear what you import. This means that it can set references to point to objects exported by the elec_meter.models module, and thus override the original references.
I'm trying to fix a circular import issue I'm having with Django tests.
My dir structure is:
app/tests:
test_user_level01.py
test_user_level02.py
Within each file I have classes to test get 200s and get 403s (among other classes). Users in level 2 should have all the get 200s that level 1 users have and users in level 1 should have all the get 403s that the level 2 users have. Thus I have a circular import.
Normally I would solve this with an absolute import but I can't figure out what that would be.
I've tried:
"""test_user_level01.py"""
from . import test_user_level02
"""test_user_level02.py"""
from . import test_user_level01
But this seems to give a circular import error (missing classes).
Thank you ahead of time for your help.
PS. The following doesn't work:
import app.tests.test_user_level01 as level01
OR:
import tests.test_user_level01 as level01
OR:
import .test_user_level01 as level01
Move common code, i.e. code which is used in both test_user_level01.py and test_user_level02.py, to a separate file, e.g. app/tests/common.py, and then import from there. Don't forget to make app/tests a package (i.e. create __init__.py file in the dir).
"""test_user_level01.py"""
from app.tests.common import some_common_class_name
"""test_user_level02.py"""
from app.tests.common import some_common_class_name
I'm not sure why it won't work, it may be an issue that you can't work around, but I would just like to know why it won't work. I am sorry if I waste your time, or didn't ask the question properly, I'm 16 and new to Python kind of.
in main.py
from day_one import day_one_def
in day_one.py
from main import main_home_window
error message
ImportError: cannot import name 'day_one'
It looks like you have a circular import: main imports from day_one and day_one imports from main. This isn't really how python is supposed to work. You should create linear dependencies, where the top module only relies on the ones below it, which only rely on the ones below them, etc.