I've been trying to follow django tutorials and get started with python for the past few days, but I keep getting stuck at the same point in each one and can't seem to find a solution.
I have a directory that represents a django 'app', in this case called rango.
Inside of rango I have a views.py file and a urls.py file. I also have __init__.py in the same folder, so my directory looks like this:
rango
│
├── __init__.py
├── views.py
└── urls.py
Now when I add:
from rango import views
to the urls.py file I get an error 'No module named rango'
I read a bunch of other questions and people suggested adding
import sys
sys.path.append(".")
but that didn't change anything. Still getting the same error.
Anyone have any idea what could be going on?
Using python 2.7 on windows
Consider using a relative import.
In urls.py:
from . import views
In __init__.py:
__package__ = 'rango'
Related
Here's my current folder structure
.
├── api
│ ├── api_routes.py
│ └── sql
│ └── models.py # import db into this file
├── application
│ └── __init__.py # db here
└── wsgi.py
In __init__.py there's a variable db (Flask-SQLAlchemy instance) and a function create_app, all of which are successfully imported into wsgi.py using this line:
from application import create_app
I used the same line to import db into models.py, to no avail. What can I do now? I have no idea where to start. One SO post suggests that maybe there's a circular import involved, however, I can't find it in my code. I also tried to import with these lines without success:
from . import create_app
from .application import create_app
from ..application import create_app
Edit: after 1 week turning a way from the problem, I found the line that causes it all. The problem was indeed circular dependency. Thanks for all your help!
There are few tricks to handle such imports,
1) Mark the "application" folder as the "Sources Root", and then I think it should work (marking folder as sources root is relatively easy when using pycharm so I suggest you to use pycharm for this and for many more tricks with python)
2) trick number to is to add the path to the directory you want to import from to sys.path, do something like
import sys
sys.path = ['/path/to/your/folder'] + sys.path
...
other code
and then importing should work.
I hope this will help (:
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 have a Django project (Python 2.7.15) with the following structure:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
utils.py
utils/
__init__.py
filters.py
In my utils/filters.py file I have a class MyFilter. From polls/admin.py, however, when I try to run from utils.filters import MyFilter, I get ImportError: No module named filters. How can I import my custom filter inside the polls app without renaming the polls/utils.py module or the utils package?
NOTE: This it's not a circular import problem. This happens even if I don't import anything from utils/filters.py. It's a name conflict between utils/ and polls/utils.py. Python tries to find filters.MyFilter inside polls/utils.py and it doesn't find it so it throws the error. I just want to figure out a way to bypass this conflict and force python to look for filters.MyFilter inside the utils/ package in the project root.
In Python 2, import utils is ambiguous because it can be a relative or an absolute import.
If you enable the Python 3 behaviour by adding the following import to the top of your module,
from __future__ import absolute_import
then from utils.filters import MyFilter will be treated as an absolute import and will work.
Once you have added the future import, you would need to use an explicit relative import import .utils if you wanted to import polls/utils.py from polls/admin.py.
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
I have a Django project with the following structure
project/
package1/
__init__.py
api.py
views.py
package2/
__init__.py
api.py
views.py
__init__.py
urls.py
wsgi.py
In package1/api.py I perform the following import:
from .views import my_func
When I do this, the __name__ value in my package1/views.py module is project.package1.views, this is exactly what I want.
However when I do the same thing in package2/api.py:
from .views import my_func2
I get package2.views for __name__, it's missing the project prefix.
I can fix this by using the full from project.package2.views path, but I don't understand why it would work for one package but not another.
I don't have anything at all in my __init__ files that would alter how the imports work, either.