ImportError: No module named lop.models - python

I know there are a lot of questions related to this, but those answers don't seem to work in my situation. I'm new to Django (I've done the tutorial), but I'm fixing someone else's code who I can no longer contact.
I'm running django 1.5 on Debian with python 2.7.
I received this error.
File "views-full.py", line 1, in <module>
from lop.models import File, V1, V2
ImportError: No module named lop.models.
views-full.py:
from lop.models import File, V1, V2
...
My tree is this (to save time, my views-full.py is under lop):
Main
├── Main
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── manage.py
├── lop
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_migrate.py
│   │   ├── 0001_migrate.pyc
│   │   ├── 0002_migrate.py
│   │   ├── 0002_migrate.pyc
│   │   ├── 0003_auto__add_category.py
│   │   ├── 0003_auto__add_category.pyc
│   │   ├── 0004_auto__add_field_script_category.py
│   │   ├── 0004_auto__add_field_script_category.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── viewsb.py
│   │   ├── viewsb.pyc
│   │   └── viewsb.py.save
│   ├── views-full.py
│   ├── views.pyc
│   ├── views.py.save
│   └── views-test.py
├── scripts [39 entries exceeds filelimit, not opening dir]
├── sqlite3.db
├── static [29 entries exceeds filelimit, not opening dir]
├── templates
│   ├── entry2-full.html
│   ├── entry2.html
│   ├── entry3-full.html
│   ├── entry3.html
│   ├── entry.html
│   ├── index.html
│   ├── index.html.old
│   ├── scriptlist.html
│   └── testData.html
└── user-dirs [109 entries exceeds filelimit, not opening dir]
As you see, both my __init__.py and models.py are in the same folder (which I know that them not being there was the problem in other cases).
settings.py:
...
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
# 'django.contrib.admindocs',
'lop',
'south',
)
...
I feel like I'm making some rookie mistake, but I can't figure it out.

I am not familiar with django, but here's what I do in that kind of situation:
import sys
sys.path.append('/path/of/folder/where/module/is/')
from new_module import new_function

So I figured out the problem which was from a fault on my part.
For the longest time, I thought that to test for any compilation errors (syntax), I would run "python < filename > migrate" or "python < filename > makemigrations" on the shell. My belief was that all changes needed to be migrated and not just ones in the database. I get errors messages when syntax mistake including this one and it has worked in other cases before. Someone eventually pointed out to me that is NOT how you test it and running "python manage.py runserver 0.0.0.0:8000" would point out any errors in the code in your local machine. I got no error messages for my specific file after that.
Gabriel L'Heureux, I still appreciated your help (I would give you a point up for it but I don't have enough points to do so) so you have my thanks.

Related

Template not found after deploying to Heroku

After reading through this question, I have some insight as to why when trying to launch my app after deploying to Heroku, I get the following error: raise TemplateNotFound(template), jinja2.exceptions.TemplateNotFound: index.html.
They are saying that the templates should be at the project directory root as shown here. Is there anyway I can modify where Heroku is trying to look for the templates directory? I modified my project structure since my app was growing in size and starting to look a bit unorganized.
Here is a look at my current project structure:
Project
│
├── App
│   ├── DbMs
│   │   ├── db_actions.py
│   │   └── user_operations.py
│   ├── UI
│   │   ├── static
│   │   │   ├── css
│   │   │   │   └── main.css
│   │   │   ├── fonts
│   │   │   │   └── Gotham_Medium_Regular.json
│   │   │   ├── images
│   │   │   │   ├── favicons
│   │   │   │   │   ├── android-chrome-192x192.png
│   │   │   │   │   ├── android-chrome-512x512.png
│   │   │   │   │   ├── apple-touch-icon.png
│   │   │   │   │   ├── favicon-16x16.png
│   │   │   │   │   ├── favicon-32x32.png
│   │   │   │   │   ├── favicon.ico
│   │   │   │   │   └── site.webmanifest
│   │   │   │   ├── header.jpeg
│   │   │   │   ├── logo.png
│   │   │   │   └── radar-chart.png
│   │   │   └── javascript
│   │   │   ├── FontLoader.js
│   │   │   ├── TextGeometry.js
│   │   │   ├── create.js
│   │   │   ├── exploreMusic.js
│   │   │   └── tracks.js
│   │   └── templates
│   │   ├── base.html
│   │   ├── create.html
│   │   ├── index.html
│   │   ├── information.html
│   │   └── tracks.html
│   ├── __init__.py
│   ├── authenticate.py
│   ├── routes.py
│   ├── services.py
│   └── templates
├── Pipfile
├── Procfile
├── README.md
├── Testing
│   ├── __init__.py
│   └── testing.py
├── __pycache__
│   ├── authenticate.cpython-39.pyc
│   ├── config.cpython-39.pyc
│   ├── main.cpython-39.pyc
│   ├── services.cpython-39.pyc
│   └── user_operations.cpython-39.pyc
├── config.py
├── package-lock.json
├── package.json
├── requirements.txt
├── run.py
└── setup.py
For more context, I have everything running perfectly fine locally, I only see this error after deployment. Thanks to anyone who can provide insight.
EDIT: I tried moving the templates folder into the App directory as well as the project root and changing the following line in __init__.py both times:
template_dir = os.path.abspath('../Project/templates/')
After deploying each time, I changed the location of templates, still see the same error from Heroku: raise TemplateNotFound(template) jinja2.exceptions.TemplateNotFound: index.html
So it turns out that Heroku is picky with modifying the location of the templates & static folders.
Whats was very misleading about this issue was that I could run everything fine locally but as soon as I deployed to Heroku, the entire App would break..
The way I fixed this was by moving templates & static into the App directory. I also had to make sure that i changed the following line from:
template_dir = os.path.abspath('../Soulify/App/templates/')
static_dir = os.path.abspath('../Soulify/App/UI/static/')
app = Flask(__name__, template_folder=template_dir, static_folder=static_dir)
to:
app = Flask(__name__)
Also note that the template folder has to be named templates exactly or Heroku will fail. Again, pretty annoying.

Django error with test module while running tests

When i run python manage.py tests i get an error saying that some test module is not found.
I am using PyCharm, Django 2.1.4 and W10 on Ubuntu.
The error:
======================================================================
ERROR: projectname.projectname (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: projectname.projectname
Traceback (most recent call last):
File "/usr/lib/python3.6/unittest/loader.py", line 462, in _find_test_path
package = self._get_module_from_name(name)
File "/usr/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
__import__(name)
ModuleNotFoundError: No module named 'projectname.projectname'
What I've tried
python manage.py runserver and it runs just fine.
Add projectname to INSTALLED_APPS
Create and app called tests
My project structure
Django
│   ├── requirements.txt
│   └── projectname
│   ├── __init__.py
│   ├── manage.py
│   └── projectname
│   ├── apps
│   │   ├── accounts
│   │   │   ├── admin.py
│   │   │   ├── apps.py
│   │   │   ├── __init__.py
│   │   │   ├── migrations
│   │   │   │   ├── __init__.py
│   │   │   ├── models
│   │   │   │   ├── __init__.py
│   │   │   │   ├── profiles.py
│   │   │   │   └── users.py
│   │   │   ├── serializers
│   │   │   │   └── __init__.py
│   │   │   ├── tests.py
│   │   │   ├── urls.py
│   │   │   └── views
│   │   │   └── __init__.py
│   │   ├── __init__.py
│   ├── db.sqlite3
│   ├── __init__.py
│   ├── settings
│   │   ├── base.py
│   │   ├── development.py
│   │   ├── production.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   └── wsgi.py
I just want to run my tests like in any other django project...
I have never encountered this problem before so any help is appreciated! :)
Well, well...it turns out that changing the folder was the solutio, though i have projects working that share the same folder name so i don't really know what happend with this one.
Before:
Django
│ └── projectname
│ └── projectname
After:
Django
│ └── othername
│ └── projectname

How to run pip installed elasticsearch?

I'm trying to use Django + Haystack + Elasticsearch.
So I installed Elasticsearch 2.4 with pip becouse Django gave me errors about that it can't import Elasticsearch. Now it can and I can run ./manage.py rebuild_index in my Django project and it gives this output:
Indexing 23 journal entrys
GET /haystack/_mapping [status:404 request:0.006s]
but only if I somehove run elasticsearch. So I installed elsasticsearch2 from AUR packages as well and run that. But as I suspected when I try to get all documents by running: curl -X GET "localhost:9200/_cat/indices?v" which returns:
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open haystack 5 1 0 0 795b 795b
If I understand correctly it is empty.
I went where pip installs packages(/usr/lib/python3.6/site-packages) and found two folders related to Elasticsearch:
elasticsearch
├── client
│   ├── cat.py
│   ├── cluster.py
│   ├── indices.py
│   ├── ingest.py
│   ├── __init__.py
│   ├── nodes.py
│   ├── __pycache__
│   │   ├── cat.cpython-36.pyc
│   │   ├── cluster.cpython-36.pyc
│   │   ├── indices.cpython-36.pyc
│   │   ├── ingest.cpython-36.pyc
│   │   ├── __init__.cpython-36.pyc
│   │   ├── nodes.cpython-36.pyc
│   │   ├── snapshot.cpython-36.pyc
│   │   ├── tasks.cpython-36.pyc
│   │   └── utils.cpython-36.pyc
│   ├── snapshot.py
│   ├── tasks.py
│   └── utils.py
├── compat.py
├── connection
│   ├── base.py
│   ├── http_requests.py
│   ├── http_urllib3.py
│   ├── __init__.py
│   ├── pooling.py
│   └── __pycache__
│   ├── base.cpython-36.pyc
│   ├── http_requests.cpython-36.pyc
│   ├── http_urllib3.cpython-36.pyc
│   ├── __init__.cpython-36.pyc
│   └── pooling.cpython-36.pyc
├── connection_pool.py
├── exceptions.py
├── helpers
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   └── test.cpython-36.pyc
│   └── test.py
├── __init__.py
├── __pycache__
│   ├── compat.cpython-36.pyc
│   ├── connection_pool.cpython-36.pyc
│   ├── exceptions.cpython-36.pyc
│   ├── __init__.cpython-36.pyc
│   ├── serializer.cpython-36.pyc
│   └── transport.cpython-36.pyc
├── serializer.py
└── transport.py
elasticsearch-2.4.1.dist-info
├── DESCRIPTION.rst
├── INSTALLER
├── METADATA
├── metadata.json
├── pbr.json
├── RECORD
├── top_level.txt
└── WHEEL
I don't see a start_elasticsearch.sh or bin/elasticsearch so how can I start it?

Django template not found in main project directory error

I am getting a 'template not found' error, although I've set up a correct template hierarchy (or so I thought)
.
├── manage.py
├── twinja
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   └── views.py
└── twinjasite
├── __init__.py
├── __init__.pyc
├── settings.py
├── settings.py~
├── settings.pyc
├── static
│   └── twinja
│   ├── fonts
│   │   └── bootstrap
│   │   ├── glyphicons-halflings-regular.eot
│   │   ├── glyphicons-halflings-regular.svg
│   │   ├── glyphicons-halflings-regular.ttf
│   │   ├── glyphicons-halflings-regular.woff
│   │   └── glyphicons-halflings-regular.woff2
│   ├── images
│   ├── javascripts
│   │   ├── bootstrap
│   │   │   ├── affix.js
│   │   │   ├── alert.js
│   │   │   ├── button.js
│   │   │   ├── carousel.js
│   │   │   ├── collapse.js
│   │   │   ├── dropdown.js
│   │   │   ├── modal.js
│   │   │   ├── popover.js
│   │   │   ├── scrollspy.js
│   │   │   ├── tab.js
│   │   │   ├── tooltip.js
│   │   │   └── transition.js
│   │   ├── bootstrap.js
│   │   ├── bootstrap.min.js
│   │   └── bootstrap-sprockets.js
│   └── stylesheets
│   ├── framework.css
│   └── styles.css
├── templates
│   └── twinja
│   ├── base.html
│   └── menu.html
├── urls.py
├── urls.py~
├── urls.pyc
├── views.py
├── views.py~
├── views.pyc
├── wsgi.py
└── wsgi.pyc
At first I set up templates/base but that did not work. So I made it as you see here: templates/twinja/base
Ideally I'm setting up the MAIN template files which are independent of apps, which I thought was meant to go in the main folder (where settings.py is) but perhaps I am mistake.
Yes, I have 'twinja' set up in installed apps as well.
What appears to be wrong here?
TemplateDoesNotExist at /
twinja/base.html
If your template is independent of apps, it shouldn't be in your app folder - namespace it accordingly.
How do you load your template? Did you setup your templates/staticfolders in your settings.py?
Updated.
For the fresh project you need to configure your settings.py file. Read here, ask away if you still have a question.
Updated.
After you get familiar with static files and how to manage those, spend some time here.

Not able to run python file which is under django project

I have my project tree like .
├── sizer
│   ├── manage.py
│   ├── node
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── models.py
│   │   ├── models.pyc
│   │   ├── node_serializer.py
│   │   ├── node_serializer.pyc
│   │   ├── part_serializer.py
│   │   ├── part_serializer.pyc
│   │   ├── Part_Serializer.pyc
│   │   ├── test.py
│   │   ├── test.pyc
│   │   ├── tests.py
│   │   ├── tests.pyc
│   │   ├── urls.py
│   │   ├── urls.pyc
│   │   ├── views.py
│   │   └── views.pyc
│   ├── requirement.txt
│   ├── sizer
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── settings.py
│   │   ├── settings.pyc
│   │   ├── urls.py
│   │   ├── urls.pyc
│   │   ├── wsgi.py
│   │   └── wsgi.pyc
│   ├── solver
│   │   ├── attrib.py
│   │   ├── attrib.pyc
│   │   ├── cap.py
│   │   ├── cap.pyc
│   │   ├── __init__.py
│   │   ├── node.py
│   │   ├── node.pyc
│   │   ├── nodes1.json
│   │   ├── nodes2.json
│   │   ├── parts.json
│   ├── strings.py
│   ├── strings.pyc
│   └── workload
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
I have created node and workload app by manage.py startapp command .
In the above directory structure I copied solver .Now I am importing my node.model under sizer.py file like .
import json
from pulp import *
from attrib import *
from cap import *
from node import *
from wl import *
from sizer.node.models import Part,Node
When I run python solver/sizer.py I am keep on getting
ImportError: No module named node.models
Please help me out what might I am doing wrong here .Spent more then 4 hours still not able to figure out .
Thanks
If your app name is node then your import statement should look like:
from node.models import Part, Node
Note that this requires that you already included node in the INSTALLED_APPS in your settings.py.
There are multiple reasons why an import might fail.
The module is not on the path. To check this print sys.path in your script just before you import the module.
The module is broken and cannot be imported. You can check this by opening a Python console in the same directory as the module and attempting to import. Does that work?
Importing the module results in a CIRCULAR import. This means that the import imports another module that imports another module that imports the original module. This is easy enough to avoid with a little thought and a clear hierarchy.
So, which problem do you have? I have no idea because I can't see the sys.path, and I can't see the code in your files.
What I can see is a bit of a mess. You have multiple modules named 'node'. You have manage.py files at multiple levels. You've included the .pyc files in the output instead of editing them out for the reader. You have so many different modules called 'node', 'sizer' or 'solver' that it must be VERY confusing to figure out which one is being imported at any given time.
Your underlying problem might be that you are trying to work on a project without using source control (git) which means you don't know what changes broke things and you don't feel brave about making big changes because you have no way of stepping back in time if they don't work out.

Categories