I've been trying to import a sub-package from a main package from another sub-package in a quite different folder hierarchy.
It's specifically from a file "predict.py" under web/flask trying to import gvtools under greenvision/
I've tried the following methods:
from ...greenvision.gvtools import *
from ...greenvision import gvtools
import gvapps.greenvision.gvtools
from gvapps.greenvision.gvtools import *
from gvapps.greenvision import gvtools
All of them give the error described in the title or "no module named gvapps". I'm not sure this package structure works. What could be the issue?
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.
helloPython
__init__.py
myutil
__init__.py
mymaths.py
service
__init__.py
cal.py
mymaths.py
def myadd(a, b):
return a+b
cal.py
from ..myutil import mymaths #or any other similar import statement
sum = mymaths.myadd(3, 4)
Here, in cal.py, I want to use a method defined in mymaths.py as above
However when I try to import, I get below error, when I "Run Python file in terminal" in VSCode Tried multiple ways
First method
from ..myutil import mymaths
ImportError: attempted relative import with no known parent package
Second method
from helloPython.myutil import mymaths
ModuleNotFoundError: No module named 'helloPython'
Relative imports in Python 2.7 and ImportError: attempted relative import with no known parent package helped me to understand. Thanks to #napuzba
and #martineau
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
So I am having trouble importing classes in the same directory and getting them to work properly.
I currently have the following hiearchy
BBDriver.py
bbsource:
BouncyBallEnv.py
Console.py
resources:
misc:
objects:
Ball.py
Platform.py
My problem is between the 2 files in the bbsource directory. I have figured out how to get access from the bbsource directory down to the classes in the objects directory and vice versa but when I try to from BouncyBallEnv import BouncyBallEnv in the Console class I get the following error:
File "E:\PycharmProjects\BouncyBallPythonV0\bbsource\Console.py", line 5, in
from BouncyBallENV import BouncyBallEnv
ImportError: cannot import name 'BouncyBallEnv'
I have tried several things like:
from bbsource import BouncyBallEnv
from bbsource.BouncyBallEnv import BouncyBallEnv
But I can't get it to work.
The only time I could get it to work is when I did the following:
import bbsource.BouncyBallEnv
#Extra
print(bbsource.BouncyBallEnv.BouncyBallEnv.WIDTH)
But there must be a better way to do it than that so that I wouldn't have to type that lengthy statement that is in the print statement every time that I want to use a static variable in BouncyBallEnv.
I am still quite confused on how the Python importing works so I'm not sure how to go about doing this. Thank you.
NOTE: Running Python 3.5.1
the thing you need is aliases :
import bbsource.BouncyBallEnv as bbe
#Extra
print(bbe.WIDTH)
and you can't import a module with the from ... import ... syntax. Only attributes. It work like this :
import <module> [as <alias>]
or
from <module> import <attribute> [, <attribute2>...] # import some attributes
from <module> import * # import everything
with the second one, you could have done :
from bbsource.BouncyBallEnv import WIDTH
# the variable WIDTH is directly loaded : watch out for collision !
print(WIDTH)
It is abosolue_import rule.
try
from .BouncyBallENV import BouncyBallEnv
to access module in relative position.
besides, there should be an __init__.py file under bbsource directory