Django ImportError on Heroku - django

I tried several things to get my app working on heroku, but now I'm out of ideas. I can install my project on heroku's rep, but I get a 500 error code. My application works very well using virtualenv on my machine after I followed the steps described on heroku documentation for django.
When I do my "git push heroku master" and try in browser, I get the following error:
2013-07-07T15:39:11.170514+00:00 app[web.1]: ImportError: No module named apps.base
2013-07-07T15:39:11.170059+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/dateformat.py", line 35, in format
2013-07-07T15:39:11.170202+00:00 app[web.1]: app = import_module(appname)
2013-07-07T15:39:11.170202+00:00 app[web.1]: default_translation = _fetch(settings.LANGUAGE_CODE)
2013-07-07T15:39:11.170202+00:00 app[web.1]: _default = translation(settings.LANGUAGE_CODE)
I tought it was caused by a directory structure that wasn't supported on heroku, so I adjusted it from the default one that was created with django startproject command.
Here is my new file structure. I adjusted the import reference everywhere and as I said, I works pefctly in local:
manage.py
Procfile
requirements.txt
vielfaltig
|____apps
| |____base
| | |____models.py
| | |____templates
| | |____tests.py
| | |____urls.py
| | |____views.py
| |____projects
| | |____admin.py
| | |____models.py
| | |____templates
| | |____templatetags
| | |____tests.py
| | |____translation.py
| | |____urls.py
| | |____views.py
|____locale
|____media
|____settings.py
|____static
|____urls.py
|____vielfaltig.db
|____wsgi.py
As you notice, I have 2 apps (base and projects). In the code, I import them using "vielfaltig.apps.base" for example. I changed this everywhere. I had this error before and I changed the directory structure according to what I read when I googled the error. I also tried to put everything in the root directory (along with the requirements.txt and procfile). I don't know why it keeps telling me an ImportError for "apps.base" while I reference the app using "vielfaltif.apps.base" everywhere... ?
Does anyone have an idea? I will paste my settings.py if needed. For now I think it would just take a lot of space.
Thank you very much for any help !

I guess it will work with import vielfaltig.apps.base instead of apps.base. It can be fixed if you insert path vielfaltig to python system path in your settings.py
import sys
sys.path.append('vielfaltig')

Related

imcompatible architecture error when importing BigQueryExecuteQueryOperator

I'm trying to import the BigQueryExecuteQueryOperator on my local Airflow, installed on the M1 macbook.
from airflow.providers.google.cloud.operators.bigquery import BigQueryExecuteQueryOperator
The import returns this error:
| "/Library/Frameworks/Python.framework/V
| ersions/3.10/lib/python3.10/site-packag
| es/pandas/util/_decorators.py", line
| 14, in <module>
| from pandas._libs.properties import
| cache_readonly # noqa:F401
| File
| "/Library/Frameworks/Python.framework/V
| ersions/3.10/lib/python3.10/site-packag
| es/pandas/_libs/__init__.py", line 13,
| in <module>
| from pandas._libs.interval import
| Interval
| ImportError:
| dlopen(/Library/Frameworks/Python.frame
| work/Versions/3.10/lib/python3.10/site-
| packages/pandas/_libs/interval.cpython-
| 310-darwin.so, 0x0002): tried:
| '/Library/Frameworks/Python.framework/V
| ersions/3.10/lib/python3.10/site-packag
| es/pandas/_libs/interval.cpython-310-da
| rwin.so' (mach-o file, but is an
| incompatible architecture (have
| (x86_64), need (arm64e)))
I did some googling and updated my google-cloud-bigquery and grpcio with pip, but the error persists.
Any help will be appreciated. Thanks!
The problem is not with Airflow or GCP provider, it's with pandas and nunpy, you should install the version compatible with the ARM64 architecture (apple M1 for ex), here is an example of a code which I used to solve a similar problem one year ago:
pip3 install Cython
pip3 install --no-binary :all: --no-use-pep517 numpy
pip3 install pandas
pip3 install ib_insync
I work in a M1 Macbook.
For me in my local virtual env, I only have to add the Apache Airflow GCP package :
requirements.txt file
apache-airflow[gcp]==2.3.4
In shell terminal :
pip install -r requirements.txt
To manage Python versions correctly in my machine I use PyEnv : https://github.com/pyenv/pyenv
You can also check the following links that can help with details about environment :
https://github.com/pyenv/pyenv/wiki#suggested-build-environment
https://github.com/pyenv/pyenv/wiki/Common-build-problems

How to import multiple class objects from seperate folders?

I built a python 2.7 application with the below directory structure for my relevant files. How do cal methods located in different folder locations?
Data-Wrangling-OpenStreetMap-data
|
+---- process_data
| |
| +---- __init__.py
| |
| +---- data_cleaner.py
|
+---- main_code.py
|
+---- audit _data
| |
| +---- __init__.py
| |
| +---- audit_file.py
I have succeeded in doing it correctly for one class referenced from main_code.py via the use of:
from process_data.data_cleaner import DataCleaner
However, if attempt a similar pattern for another class located in seperate folder referenced by main_code.py for via the use of the import statement
from audit_data.audit_file import AuditFile
I get the error:
ImportError: No module named audit_data.audit_file
Any ideas as to what I may be overlooking and/or guidance on what further details I need to post to help find the solution to my problem?
from process_data.data_cleaner import data_cleaner
as data_cleaner is the file name data_cleaner.py and the second one data_cleaner is a class defined in it.
The cause of my problem was a silly one;
The folder containing the class I was calling was named audit _file whilst the folder I was calling within my code was audit_file
What didnt work
from audit_data.audit_file import AuditFile
What worked
from audit _data.audit_file import AuditFile
For those reading this watch out for unintended spaces in your folder names

How solve"no python application found check your startup logs" error for Django + uWSGI + nginx stack

I user Django 1.10 with uWSGI and nginx on ubuntu 16.04 and deploy my app with ansible. My project have not default structure, but quite common ( thank Two scoopce for this :).
I use split dev and production settings and config folder instead 'name' project folder. It's looks like this:
|-- config
| |-- __init__.py
| |-- settings
| | |-- __init__.py
| | |-- base.py
| | `-- dev.py
| |-- urls.py
| |-- wsgi_dev.py
| `-- wsgi_production.py
|-- manage.py
`-- requirements.txt
My production.py genarate from ansible with security encrypt and locate in config/settings.
With this config i get "no python application found check your startup logs". Uwsgi don't see my application.
( {{ }} it's jinja2 syntax for ansible )
/etc/uwsgi/sites/{{ project_name }}
[uwsgi]
chdir = {{ django_root }}
home = /home/{{ project_user }}/venvs/{{ project_name }}
module = config.wsgi_production:application
master = true
processes = 5
socket = /run/uwsgi/{{ project_name }}.sock
chown-socket = {{ project_user }}:www-data
chmod-socket = 660
vacuum = true
After several weeks i can find problem in my wsgi.py. It common solution use os.environ['ENV'] for DJANGO_SETTINGS_MODULE, but with deffrent users and permissions its dosen't work.
If you use in your wsgi.py file something like this:
os.environ["DJANGO_SETTINGS_MODULE"] = "config.settings." + os.environ["ENV"]
And have problem with no python application found - split your wsgi file. I can catch that os.environ["ENV"] return empty string. I add it for my all user, use source and etc. But uwsgi in emperior mode don't see it.
You sould use wsgi_dev.py and wsgi_production.py where you can write somethink like this os.environ["DJANGO_SETTINGS_MODULE"] = "config.settings.production". It's not so elegant but solve this problems fine.
For use splitting wsgi you can write something like this in wsgi.py
import os
from django.core.wsgi import get_wsgi_application
if os.environ.get('DEV') is True:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.dev")
else:
os.environ.setdefault("DJANGO_SETTINGS_MODULE",
"config.settings.production")
application = get_wsgi_application()

why Django put application at the same level of my project folder?

since Django 1.4 (I think) django create a folder for my project when I start a project. Django add a folder for any application I created (with python manage.py startapp) at the same level of my project folder.
Project_name
|---project_name_dir/
|---application_dir/
`---manage.py
I really like the following folder structure:
Project_name
|---project_name_dir/
| |---application_dir/
| | |-- __init__.py
| | |-- models.py
| | |-- tests.py
| | `-- views.py
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| |-- wsgi.py
| |---templates/
| | `---application_dir/
| `---static/
| |---css/
| |---font/
| |---img/
| `---js/
|---deployment/
|---documentation/
|---config/
`---manage.py
Because I have a folder with all my django files (project_name_dir/) and other directories for non django files.
So why Django put application at the same level of my project folder?
In Django, the position of the application directory is not considered. Django only uses the name of the application.
Thus, the position of the application is basically a matter of convenience of the programmer.
This is also the reason why two apps should not have the same name: even if they are imported in INSTALLED_APPS as
('app.app1', 'app1')
Django only concerns with the last part after the dot, i.e. app1.
So, in the end, you can use the directory structure you want, as long as the apps' names don't collide and you point to the app on INSTALLED_APPS. Because of this, if there isn't any special reason, you should put them on the project's root, like Django does.

Setting up Yeoman with Django

Has anyone being able to get Yeoman to work with Django??
I've tried to set it up and even if i change my grunt file to the correct paths its still uses the default.
I've searched only but it doesnt that anyone is using such file structure.
So that was a really stupid comment I made above. :-)
Here's a proper response! Yeoman is simply a scaffolding tool for us to quickly generate css, js and html files. I am using it in a completely decoupled way, cleanly separated from django.
Here's the tree structure of the frontend site.
/Users/calvin/work/yeoman-test/
|~app/
| |~scripts/
| | |~controllers/
| | | `-main.js
| | |~vendor/
| | | |-angular.js
| | | |-angular.min.js
| | | |-es5-shim.min.js
| | | `-json3.min.js
| | `-app.js
| |~styles/
| | |-bootstrap.css
| | `-main.css
| |+views/
| |-.buildignore
| |-.htaccess
| |-404.html
| |-favicon.ico
| |-index.html
| `-robots.txt
|~test/
| |+spec/
| `+vendor/
|-.gitattributes
|-.npmignore
|-Gruntfile.js
|-package.json
`-testacular.conf.js
And here's the tree structure for the django application acting as a pure json web-service. Using django-tastypie.
/Users/calvin/work/yeomandjango/
|~deploy/
| |-crontab
| |-gunicorn.conf.py
| |-live_settings.py*
| |-nginx.conf
| `-supervisor.conf
|~requirements/
| `-project.txt
|+static/
|-.gitignore
|-.hgignore
|-__init__.py
|-__init__.pyc
|-dev.db
|-fabfile.py
|-local_settings.py
|-local_settings.pyc
|-manage.py*
|-settings.py
|-settings.pyc
|-urls.py
`-urls.pyc
By running the django web service from domain and urls such as http://service.mysite.com/api/v1/ and having our frontend yeoman generated "static" site http://mysite.com calling these API urls as needed.
The yeoman generated AngularJS app simply POSTS/GETS/PUTS/DELETES the api resources/urls given by our django-tastypie APIs.
This is a loosely coupled configuration you can consider.
However, do note that this set-up is performing "cross-domain API requests". This means that on our "server-side" django application, we will need to handle CORS.
Here's an example middleware snippet that needs to be implemented at django server side for this to work.
import re
from django.utils.text import compress_string
from django.utils.cache import patch_vary_headers
from django import http
try:
import settings
XS_SHARING_ALLOWED_ORIGINS = settings.XS_SHARING_ALLOWED_ORIGINS
XS_SHARING_ALLOWED_METHODS = settings.XS_SHARING_ALLOWED_METHODS
except:
XS_SHARING_ALLOWED_ORIGINS = '*'
XS_SHARING_ALLOWED_METHODS = ['POST','GET','OPTIONS', 'PUT', 'DELETE']
class XsSharing(object):
"""
This middleware allows cross-domain XHR using the html5 postMessage API.
Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
"""
def process_request(self, request):
if 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request.META:
response = http.HttpResponse()
response['Access-Control-Allow-Origin'] = XS_SHARING_ALLOWED_ORIGINS
response['Access-Control-Allow-Methods'] = ",".join( XS_SHARING_ALLOWED_METHODS )
return response
return None
def process_response(self, request, response):
# Avoid unnecessary work
if response.has_header('Access-Control-Allow-Origin'):
return response
response['Access-Control-Allow-Origin'] = XS_SHARING_ALLOWED_ORIGINS
response['Access-Control-Allow-Methods'] = ",".join( XS_SHARING_ALLOWED_METHODS )
return response