Deploying Project in PythonAnywhere. Settings Module Import Errror - python

I´m trying to deploy my django 1.6.4 project on pythonAnywhere using python 2.7
I already configured a virtual enviroment and the wsgi file according to the guidelines on the website. But I´m getting a 404 when I check the site. The error lol tells me this:
ImportError: Could not import settings 'tango_with_django_project.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named tango_with_django_project.settings
Here´s my wsgi:
# +++++++++++ DJANGO +++++++++++
# TURN ON THE VIRTUAL ENVIRONMENT FOR YOUR APPLICATION
activate_this = '/home/pjestrada/.virtualenvs/rango/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
# To use your own django app use code like this:
import os
import sys
#
## assuming your django settings file is at '/home/pjestrada/mysite/settings.py'
path = '/home/pjestrada/rango/tango_with_django_project'
if path not in sys.path:
sys.path.append(path)
os.chdir(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'tango_with_django_project.settings'
#
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
My tree:
├── Dropbox
│ ├── README.txt
│ └── __init__.py
├── README.txt
└── rango
├── LICENSE
├── README.md
└── tango_with_django_project
├── manage.py
├── media
│ ├── profile_images
│ │ └── 10411981_634016890008979_1609187547738555774_n.jpg
│ └── rango2.jpg
├── populate_rango.py
├── rango
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── admin.py
│ ├── admin.pyc
│ ├── bing_search.py
│ ├── bing_search.pyc
│ ├── forms.py
│ ├── forms.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views.py
│ └── views.pyc
├── static
│ ├── about.jpg
│ ├── css
│ │ ├── bootstrap-fluid-adj.css
│ │ ├── bootstrap-responsive.css
│ │ ├── bootstrap-responsive.min.css
│ │ ├── bootstrap.css
│ │ └── bootstrap.min.css
│ ├── img
│ │ ├── glyphicons-halflings-white.png
│ │ └── glyphicons-halflings.png
│ ├── js
│ │ ├── bootstrap.js
│ │ ├── bootstrap.min.js
│ │ ├── jquery-2.1.1.js
│ │ └── rango-ajax.js
│ └── rango.jpg
├── tango_with_django_project
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ └── wsgi.py
└── templates
└── rango
├── about.html
├── add_category.html
├── add_page.html
├── base.html
├── category.html
├── category_list.html
├── index.html
├── login.html
├── page_list.html
├── profile.html
├── register.html
├── restricted.html
└── search.html

Try changing value of path.
path = '/home/pjestrada/rango'
That path is your project directory. It worked for me.

Related

ImportError attempted relative import beyond top-level package

This is my project structure:
.
├── connectapp
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── djangoproject
│ ├── __init__.py
│ ├── __pycache__
│ ├── asgi.py
│ ├── djangoproject.sqlite3
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── djangoproject.sqlite3
├── hackerapp
│ ├── Controllers
│ ├── Models
│ ├── Serializers
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ └── tests.py
├── manage.py
└── requirments.txt
When I do below being in top level folder:
python3 manage.py migrate hackerapp
I am getting this:
djangoproject/djangoproject/urls.py", line 19, in <module>
from ..hackerapp.Controllers import PersonViewSet, DepartmentViewSet
ImportError: attempted relative import beyond top-level package
To me looks import should work but it's not, can someone tell me why?
I guess (?) migrate hackerapp are cli params to the manage.py script. The error means that the top level folder is not a package. To make it into a package add an (empty) __init__.py file then from that folder run:
python -m manage migrate hackerapp # note no .py

Django Models disappear from app deployed in Heroku

I am building a personal portfolio website with Django which I'm hoping to host on Heroku. I am aware of the platform's ephemeral storage problem so all of the images are served from an s3 bucket. After deploying the app though and running the python manage.py migrate from the dyno and check the postgresql database on the dashboard I can see rows and columns created but they're not populated. Therefore, there are no models stored in this database. I'm not .gitignore-ing the db.sqlite3. I'm also using a virtual environment.
Ih short here's the output of the tree command from the root folder:
├── Procfile
├── db.sqlite3
├── manage.py
├── media
│ └── images
│ ├── angular.png
│ ├── bash.png
│ ├── c.png
│ ├── calibration.png
│ ├── commerce_img.png
│ ├── css3.png
│ ├── django.png
│ ├── git.png
│ ├── html.png
│ ├── image-processing-api.png
│ ├── js-logo.png
│ ├── mail.png
│ ├── my_store.png
│ ├── network.png
│ ├── nodejs.png
│ ├── programmer.svg
│ ├── python.png
│ ├── rest_with_node.png
│ ├── sql.png
│ ├── typescript.png
│ └── wiki_image.png
├── portfolio
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-39.pyc
│ │ ├── settings.cpython-39.pyc
│ │ ├── urls.cpython-39.pyc
│ │ └── wsgi.cpython-39.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── requirements.txt
├── staticfiles
└── webdev_portfolio
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-39.pyc
│ ├── admin.cpython-39.pyc
│ ├── apps.cpython-39.pyc
│ ├── models.cpython-39.pyc
│ ├── tech_stack.cpython-39.pyc
│ ├── urls.cpython-39.pyc
│ └── views.cpython-39.pyc
├── admin.py
├── apps.py
├── models.py
├── static
│ └── webdev_portfolio
│ └── styles.css
├── templates
│ └── webdev_portfolio
│ ├── index.html
│ ├── layout.html
│ ├── project.html
│ └── projects.html
├── tests.py
├── urls.py
└── views.py
Finally here's my settings.py file in case I missed something:
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES['default'].update(db_from_env)
Am I missing something?
Thanks.
So you are using psql on Heroku trying to populate it with db.sqlite3 file? If that is your intention that is not going to work. Both databases are different, in fact, to populate a newly created PSQL you would need either a fixture or backup.
Check the Heroku documentation

Cannot import model from .models file to nested app sub_directory

I suspect that I am not understanding something about django. I am trying to test the file scrape.py which calls an api and parses then writes the response to the model CMC in the models.py using sqlalchemy. I am trying to test it to see if the file itself will run but (here's where I think I'm going wrong) I am pressing the play button while having the scrape.py file pulled up in vscode. I suspect there is something here that Django won't allow but am not familiar enough with django to know if that is the case. It is throwing a ModuleNotFound error as described below but I suspect that it's doing that because I am calling it because when I type the dot after apis after import at the top of the file it shows a list of all the drop-down files in my apis app.
Here is my project tree:
(base) justinbenfit#MacBook-Pro-3 cds_website % tree
.
├── api
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ ├── admin.cpython-38.pyc
│ │ ├── apps.cpython-38.pyc
│ │ ├── models.cpython-38.pyc
│ │ ├── serializers.cpython-38.pyc
│ │ ├── urls.cpython-38.pyc
│ │ └── views.cpython-38.pyc
│ ├── admin.py
│ ├── apps.py
│ ├── main.py
│ ├── management
│ │ ├── __init__.py
│ │ ├── commands
│ │ │ ├── __init__.py
│ │ │ ├── __pycache__
│ │ │ │ └── private.cpython-39.pyc
│ │ │ ├── private.py
│ │ │ └── scrape.py
│ │ └── test.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── 0001_initial.cpython-38.pyc
│ │ └── __init__.cpython-38.pyc
│ ├── models.py
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── cds_website
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ ├── settings.cpython-38.pyc
│ │ ├── urls.cpython-38.pyc
│ │ └── wsgi.cpython-38.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── requirements.txt
api is an app in a greater project called cds_website. The settings.py file in cds_website project directory contains the following installed apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'api.apps.ApiConfig',
'rest_framework',
'environ'
]
and my apps.py file in the api app contains the following class:
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'
CMC is a model in my models.py file. I am trying to import it into scrape.py. I have tried:
from ...models import CMC
from api.models import CMC
from ... import CMC
first one throws: ImportError: attempted relative import with no known parent package second one throws: ModuleNotFoundError: No module named 'api' third one throws: ImportError: attempted relative import with no known parent package
Here is a link to the code repo: https://github.com/Justinbenfit23/cds_website
Everything I have read indicates that at least one of these should work. Any direction appreciated!
I'm teste your project with from api.models import CMC and it's worked
The easiest way is to create ecrape_manage.py in cds_website-master
folder, content of ecrape_manage.py:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cds_website.settings")
import django
django.setup()
exec(open("api/management/commands/scrape.py").read())
and just run with python ecrape_manage.py

Relative import error even after adding __init__

This is the directory structure in a project I am working on:
.
├── analyx
│ ├── database.py
│ ├── db.sqlite3
│ ├── endpoints.py
│ ├── errors.py
│ ├── flow.py
│ ├── import_test_copy.py
│ ├── __init__.py
│ ├── metrics.py
│ ├── mkdocs.yml
│ ├── models.py
│ ├── plugins.py
│ ├── __pycache__
│ │ ├── endpoints.cpython-38.pyc
│ │ ├── endpoints.cpython-39.pyc
│ │ ├── errors.cpython-38.pyc
│ │ ├── errors.cpython-39.pyc
│ │ ├── flow.cpython-38.pyc
│ │ ├── flow.cpython-39.pyc
│ │ ├── __init__.cpython-38.pyc
│ │ ├── __init__.cpython-39.pyc
│ │ ├── metrics.cpython-38.pyc
│ │ ├── metrics.cpython-39.pyc
│ │ ├── plugins.cpython-38.pyc
│ │ ├── plugins.cpython-39.pyc
│ │ ├── settings.cpython-38.pyc
│ │ ├── settings.cpython-39.pyc
│ │ └── visualize.cpython-38.pyc
│ ├── settings.py
│ └── visualize.py
├── docs2
│ ├── dev_docs
│ │ └── graph.md
│ ├── flow.md
│ └── index.md
├── endpoints2.py
├── flow_test_copy.py
├── __init__.py
├── metrics2.py
├── mkdocs.yml
├── plugins
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ └── __init__.cpython-39.pyc
│ └── test_plugin
│ ├── __init__.py
│ ├── models.py
│ ├── __pycache__
│ │ ├── __init__.cpython-39.pyc
│ │ ├── models.py
│ │ └── test_plugin.cpython-38.pyc
│ └── test_plugin.py
├── __pycache__
│ ├── endpoints.cpython-39.pyc
│ └── metrics.cpython-39.pyc
├── README.md
├── requirements.txt
├── setup.py
└── tests
├── flow_test.py
├── __init__.py
└── test_dummy.py
As evident, I have added __init__.py to all the directories, so that I can import them as packages and sub packages.
The content of the file plugins/test_plugin/test_plugin.py starts as follows:
from ...analyx.plugins import Plugin
It still gives the error:
ImportError: attempted relative import with no known parent package
How do I fix it? I am not very acquainted with the nuances of the Python Packaging system, so any help will be appreciated.
Thanks in advance!

Flask blueprint doesn't work (Python2)

Setup:
I am trying to clone git project(all the code here) to locally deploy it and make it work for academic purpose.
So far I have only had experience with Flask under Python 3, but this project is written on Flask using Python 2. After setting up virtualenv, installing all requirements I can successfully run(python server.py) it and navigate to index page.
Problem: Whenever i try to reach pages like "localhost:5000/login" I can only see 404 error "Not Found". Looking through the code I see that it is importing blueprints which contain routes to ".../login" view, but it doesn't get to a point of showing it.
Project structure looks like this:
.
├── API Documentation.md
├── app.py
├── app.pyc
├── data
│ ├── samples
│ │ ├── categories.txt
│ │ ├── domains.txt
│ │ ├── names.txt
│ │ ├── products.txt
│ │ └── surnames.txt
│ └── sql
│ └── schema-00.sql
├── Makefile
├── README.md
├── requirements.txt
├── server.py
├── sfec
│ ├── api
│ │ ├── base.py
│ │ ├── base.pyc
│ │ ├── decorators.py
│ │ ├── decorators.pyc
│ │ ├── fields.py
│ │ ├── fields.pyc
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── order.py
│ │ ├── order.pyc
│ │ ├── product.py
│ │ ├── product.pyc
│ │ ├── user.py
│ │ └── user.pyc
│ ├── config.py
│ ├── config.pyc
│ ├── controllers
│ │ ├── decorators.py
│ │ ├── decorators.pyc
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── order.py
│ │ ├── order.pyc
│ │ ├── user.py
│ │ └── user.pyc
│ ├── database
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── runtime.py
│ │ ├── runtime.pyc
│ │ ├── settings.py
│ │ └── settings.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ └── models
│ ├── base.py
│ ├── base.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── order.py
│ ├── order.pyc
│ ├── product.py
│ ├── product.pyc
│ ├── user.py
│ ├── user.pyc
│ ├── views.py
│ └── views.pyc
├── sfecadmin.py
├── templates
│ └── index.html
├── tests
│ ├── __init__.py
│ └── user_test.py
├── tree.txt
└── uml_diagram.png
10 directories, 63 files
And that's how blue print is called inside of executable server.py(pieces of code):
from sfec.api.user import register_user_resource
from sfec.controllers.user import user_api
app.register_blueprint(user_api, url_prefix='/api')
And user.py file (./sfec/controllers/user.py) contain(pieces of code):
user_api = Blueprint('user_api', __name__)
#user_api.route('/login', methods=['POST'])
def login():
print "login page"
"""Log the user in."""
store = get_default_store()
user = User.authenticate(store, request.form['email'],request.form['password'])
if user:
session['user'] = user.id
return user.json()
abort(403)
The 'login' route is create, so I would expect after navigating to 'localhost:500/login' to receive something back, at least an error 403 or something, but not 404(not found) error.
Can you please help me to understand what am I missing?
I would highly appreciate any help.

Categories

Resources