How to access models/usrs from resources/user
├───models
| └───user.py
├───resources
| └───user.py
First I import it like this one:
from code.models.user import UserModel
But I got a compile-time error:
`Cannot find reference 'models' in 'code.py'`
And I tried another way like this one:
from ..models.user import UserModel
But I got a runtime error:
ImportError: attempted relative import beyond top-level package
And I added init.py in both files but still doesn't work.
And also I tried these solutions but they don't fix my issue, please help me
Add the models and resources directories to your $PYTHONPATH or use sys.path:
export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add
Or:
import sys
sys.path.insert(0, "/home/project-name/models")
Or add "__ init__.py" to code directory:
from code.models import user
And finally, I fix my issue:
As #Little Bamboo said: I used sys concept for the import like below:
import sys
sys.path.append("D:\\Projects\\Python\\UdemyProjects\\SixSimplifyingTwo")
from mycode.models.userone import UserModel
But that was not all, some of my files had the same names and I changed the names of the files.
And also I had a folder called code, and from the below link I understood that code is a built-in Python module and I changed the code folder name.
Werkzeug AttributeError: 'module' object has no attribute 'InteractiveInterpreter'
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
For some reason, I had to change a module name from A.py to 0880ceae-8a46-11eb-bcf6-38f9d349be8e.py. 0880ceae-8a46-11eb-bcf6-38f9d349be8e.py is a uid generated by uuid.uuid1().
After my change, I try to import a class B from the py file by the following two ways, both do not work out.
First solution is to import directly
from 0880ceae-8a46-11eb-bcf6-38f9d349be8e import B
It has an error SyntaxError: invalid token
Second solution is to define a variable before import
uid = '0880ceae-8a46-11eb-bcf6-38f9d349be8e'
from uid import Model_API
And it has en error ModuleNotFoundError: No module named 'uid'
Anyone has a good idea? Thanks.
Here is possible solution to your problem tested using python3:
# Modern approach
import importlib
module = importlib.import_module(".", "0880ceae-8a46-11eb-bcf6-38f9d349be8e")
module.hello()
# Deprecated way
module = __import__("0880ceae-8a46-11eb-bcf6-38f9d349be8e")
module.hello()
Both of the above methods are tested and works.
Here are the rules to keep in mind:
Check your filename should end with .py extension, .py.py will cause ModuleNotFoundError.
Make sure to not include .py extension while using in importlib module.
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
Im having trouble importing a class on a python module.
Here are my directory structure:
_wikiSpider
+scrapy.cfg
_wikiSpider
+__init__.py
+items.py
+items.pyc
+settings.py
+settings.pyc
+pipelines.py
_spiders
+__init__.py
+__init__.pyc
+articleSpider.py
+articleSpider.pyc
+items.py
Code breaks at this line:
from wikiSpider.items import Article
Im not sure why, since class Article is defined at items.py (deepest folder)
Can someone give me an explanation?
Like others, I didn't have a circular reference problem. I'd like to generalize the solution here just a bit more though.
Any file name conflict can cause this. You could have multiple sub files with the same name (as above).
Or it could be the file you're working in.
Eg: trello.py as a pet project.
from trello import TrelloApi
Import reference will import itself before importing the pip installed package. Attempts to import trello and reference objects directly will fail with "NameError: name '' is not defined"
You have an items.py in both your root and _spiders folder. To reference a file in a subfolder you need the folder name and the file.
from _spiders.items import Article
assuming the file that imports this code is in your root directory. Python uses a you are here, to current file location, for it's directory hierarchy.
Best solution :
Rename class name with temporary name
Put the same temporary name in import statement in __init__.py
Now that works, put your old name again
from main wikiSpider directive try:
from _wikiSpider._spiders.items import Article
orelse from terminal open your _spiders directive and try:
from items import Article
Here we want to open the items.py file where we created Article class, so when you give some wrong directive or file , it cant find the items.py file that you created, so it shows 'Cannot import error'
What worked for me is removing the __pycache__ folder. I was moving class files around and changing the directory hierarchy up and the cache must have had old names/locations. Deleting it and running my program again created a new cache and the error was no longer present.
I have sub-directories inside my django app folder, and on each I was trying to call a module. The issue that I am having is that I am able to import a module by using * but not by name which produces an error "Exception Value: cannot import name [my module]"
from foo import Bar # throws error
from foo import * # works
I dont know if I am missing anything on my settings.py but definitely I have included the app directory on my INSTALLED_APPS and also I have init.py on each directory. I also tried to check if my app folder is included on python paths and it was included.
Any help will be appreciated. THanks in advance
I expect you are thinking in terms of Java. In Python, you import things by module, not class name. So if a directory foo contains a file bar.py which defines a class Bar, you must do from foo.bar import Bar, not from foo import Bar.
After looking over my directories I found out that I have a file that has the same name as my app/Foo. after removing this it started working.
Foo/
- Bar.py
Process/
- Foo.py # deleted this
- views.py
from Foo import Bar #Foo.py was overriding the app/Foo that I was trying to call
Thanks for all your response!
if this is in django, try doing
from . import form
from . import models
from . import views
This should solve the issue.
Apart from that, fist make sure you have a init.py file in the directory.