I have a django project like:
virt_env_name/
project/
scripts_api/
__init__.py
scripts/
__init__.py
orders.py
api/
__init__.py
api.py
project/
settings.py
I am accessing the modules in order.py like:
from scripts_api.api.api import ClassA
from project.settings import Var1, Var2
but it is throwing the following error No module named for both imports.
How can I correct this?
NOTE: scripts_api is added in installed apps in settings.py. I thought it would help if scripts are in a django project application.
Go to setting.py and print the value of BASE_DIR
Runserver and check the terminal if the directory is the same as your working directory.
Because sometimes "No Module Found" error is raised due to BASE_DIR not being the correct one. Usually it is one or two folder outside or inside.
In your case, printing BASE_DIR should give a value like this for you to correctly access the modules:
abcd/pqrs/xyz/virtual_env_name/project
if the value doesn't ends the same way as above, then correct the BASE_DIR value in settings.py according to your requirement.
Related
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 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.
I have problem with imported module into my qsl/management/commands/<customcommand>.py file.
in fact, my app structure is :
qsl/management/commands/ : dir for my management commands
qsl/management/jobs/ : dir for my mangement jobs
jobs are python classes that contains the job i want to be done in the coresponding command
e.g:
news command in qsl/management/commands/ imports news job in qsl/management/jobs/
my error when i want to execute python manage.py news is an importerror : no module named management.jobs.news
Make sure that all the folders have a __init__.py in them so that they can be imported as modules.
The structure is described here: https://docs.djangoproject.com/en/dev/howto/custom-management-commands/
Something like this for your structure:
qsl/
__init__.py
models.py
management/
__init__.py
commands/
__init__.py
news.py
jobs/
__init__.py
news.py
tests.py
views.py
If isn’t not a typo in file/directory structure then perhaps you are pip installing and having the issue?
In your setup.py under your packages=[] make sure you include
both qsl.management and qsl.management.commands. This solved the issue for me.