I should note that there is an __init __.py file in the application directory.
I believe I know the source of the error. It seems to lie in the application's views.py file.
from tbg.myapp.models import Document
from tbg.myapp.forms import DocumentForm
Showing you as little as possible, this is a part of what I imported. I observe that if I remove the tbg, for example, the error will alter to no module named myapp.
How do I fix this?
Assuming tbg is the name of your project, your project's directory structure should be something like below:
tbg/ # your project's main folder
|-- tbg/ # inner tbg folder which contains settings, urls, etc.
| __init__.py, settings.py, etc.
|-- myapp/
models.py, etc.
So when you are writing from tbg.myapp.models import Document, you are referring to the tbg folder inside your project's main folder. As this tbg folder doesn't have myapp folder, you are getting an error.
To fix this, write from myapp.models import Document.
Related
I have a main.py file and I need to import the database tables that are in "entity / models.py". How do I do ?
I use linux and I don't want to add models.py to sys.path, I want models.py to be visible only in this project. I also don't want to create symbolic links because in my opinion it's the same thing as putting models.py inside the app itself.
You can convert the directory into a package with a __init__.py file. Then as usual you can import the module from the package as in:
from entity.models import func
The __init__.py file can just be an empty file. Just its existence will treat the directory as a package.
Hope this answer helps.
I am working on a Django app in which I want to import some class/function from generator.py into my views.py to process an user-submitted input. My folder structure looks like this:
project/
models/
__init__.py
generator.py
web/
django/
__init__.py
settings.py
urls.py
wsgi.py
django_app
__init__.py
views.py
etc.
Inside views.py, I have
from ...models.generator import Generator
When I try to run server, what I get is:
ValueError: attempted relative import beyond top-level package
I've seen many answers, but most are about manipulating sys.path or changing PYTHONPATH. I'm not sure how and where to do either cause I'm rather new to Django.
Can someone tell me exactly which commands to run to allow the import to be done?
Import of python is based on sys.path and cannot be outside the top-level dir. More information.
That's mean, if you append top of BASE_DIR path in sys.path you can import. But it's a tricky way.
def import_function():
import sys
sys.path.append(os.path.dirname(BASE_DIR))
module = __import__('models.generator') # import code
sys.path.pop()
return module
g_module = import_function()
g_module.Generator
I just started testing out PyCharm on my existing Django project, and it doesn't recognize any imports from apps within my project:
in my_app1/models.py:
from my_app2.models import thing
"Unresolved reference 'my_app2'"
Why is this? My project's directory structure matches the recommended layout, and it runs without errors, it's just PyCharm's magic doesn't want to work on it.
It seems related to this question:
Import app in django project
But I can't figure out what I am doing wrong. If I try:
from ..my_app2.models import thing
The PyCharm error goes away and it can auto predict, etc. But when I run the project Django throws:
ValueError: attempted relative import beyond top-level package
EDIT:
Project structure:
my_project/
src/
manage.py
db.sqlite3
my_app1/
templates/
__init.py__
admin.py
models.py
urls.py
views.py
...
my_app2/
templates/
__init.py__
admin.py
models.py
urls.py
views.py
...
my_project_app/
settings/
__init.py__
urls.py
...
I was having this issue using a "2 Scoops of Django" project layout, e.g.
/project_root
/project_dir
/config
/settings
/my_app
/tests
models.py
/requirements
readme.rst
The code was working, but in /tests, IntelliJ/PyCharm showed an unresolved reference:
from my_app.models import Something
I had all the __init__.py files in place. I ended up having to set the sources root to project_dir:
Right-click on project_dir, Mark Directory as > Sources Root
Now that I can take a look over you project structure I can tell you that the problem appears to be related to a missing __init__.py in your 'src' folder. Try adding an empty file named __init__.py in the root of 'src' folder.
Also, take a look to this question, I think is the same problem or a very similar one.
Hope this could be useful, cheers!
I was having this issue after I change my environment to virtualenv, so I changed my python interpreter to my current virtualenv.
Go to File > Settings > Project Interpreter.
In that window you would be able to see all packages includes on this interpreter, Django should be there.
This worked for me.
Link about: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206598665-Unresolved-Reference-Errors-for-django
ı have a django project and i need to access some of models in my folder that under the django main project folder.Let me illustrate this.
src\
main\
urls.py
models.py
view.py
lib\
__init__.py
helper.py
This is the example folder structure and i need to import some class of main app's models inside the helper.py.I tried these:
from main.models import exampleClass
from ..main.models import exampleClass
And i also tried adding a __init__.py file in the main project folder:
src\
...
main\
lib\
__init__.py
Always errors 2 kind :
1)ValueError : relative import error
2) no module name..
I need the solution and need good explanation why i failed always.Thank you so much guys.
Add __init__.py in main folder instead of src folder. Then try to import using from main.models import exampleClass. It should be working.
You don't need .. if main and lib are both django's apps, and you have registered them in INSTALLED_APPS settings.
If main and lib are in the same level that manage.py:
src/
main/
...
lib/
...
manage.py
...
You just need:
from main.models import exampleClass
How did you set $PYTHONPATH variable ? The search paths are relative to this environment variable.
So if you want to specify a path like main.models, it should contain the src directory.
Note that you can also manage it with the sys.path array.
Django normally add all the applications to sys.path. You may try to print it inside the settings.py file to have an idea.
To add a path from the settings.py file of the project, you could do something like:
import os.path
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
sys.path.append(os.path.join(BASE_DIR, "../lib"))
If for example you have a lib directory at the same level as the directory that contains the settings.py file.
I have the following folder structure
app/
app/helpers/
app/helpers/methodhelper.py
app/methods/
app/methods/method.py
and I'm trying to import a function from methodhelper.py inside method.py
so I tried the following:
import app.helpers.methodhelper
OR
from app.helpers.methodhelper import function1
OR
import helpers.methodhelper
and I get:
"No module named app.helpers.methodhelper"
Important to note: helpers/__init__.py already exists
How should this be done ?
Your Django project's default path is in the root directory of the project (where the manage.py file is). You can either add the sub directories below that to your PYTHONPATH (easily done by appending to sys.path) or you can import that function using the full module path:
from projectname.app.helpers.methodhelper import function1
When I start a Django project, I always add
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
to my settings.py. This path looks similar to /home/kyle/django_project_name/. Inside that directly is manage.py.
From there, also in my settings.py, I include:
sys.path.append(os.path.join(PROJECT_ROOT, 'django_project_name'))
This makes my apps importable without the need to include my project name in the module path.
you need to add the module in settings.py file