database configuration in pyramid - python

I tried to create a sqlalchemy project in pyramid and when I run the server, I get this error,
Pyramid is having a problem using your SQL database. The problem
might be caused by one of the following things:
1. You may need to run the "initialize_MyProject_db" script
to initialize your database tables. Check your virtual
environment's "bin" directory for this script and try to run it.
2. Your database server may not be running. Check that the
database server referred to by the "sqlalchemy.url" setting in
your "development.ini" file is running.
After you fix the problem, please restart the Pyramid application to
try it again.
when I check my development.ini file the sqlite database is configured as this,
sqlalchemy.url = sqlite:///%(here)s/MyProject.sqlite
What needs to changed in here to configure it correctly?
I run on linux box.

You need to create a database in either sqlite,postgres,or any other,Thereafter go to development.ini file edit sqlalchemy.url = sqlite:///%(here)s/MyProject.sqlite and specify the name of your database,then run the initialize_myproject_db development.ini command.if you are using mysql, that line should be
sqlachemy.uri = mysql://username:password#host/dbname

Just trying Pyramid for the first time, I just faced the same problem, after many command combinations, I just got the solution.
Run from the project root, the command:
initialize_tutorial_db development.ini
Info taken from Wiki2 SQLAlchemy tutorial

It says right there in the first point - you need to run initialize_MyProject_db development.ini to create database.
If that's not the case please post the log from running the server.

Related

how to run existing Django project

I'm new with python(don't know anything about it), and also in Django... so question is, what and how, step by step, I need to launch existing Django project which I got from github...
so, what I have done already:
installed python
created virtual environment
run python mange.py migrate, but it showed many errors(and I don't know what to do with it and how to fix them), because I can't even connect to DB(previously I worked with MySQL where I can connect by myself and do selects to check something in DB), but now this project uses SQLite3, and I don't know how I can check this database
run python manage.py runserver and I can see my webapp at localhost...
So, main question is, how to properly launch existing Django project?

Flask & SQLAlchemy - Command Line & Shell use correct database connection, but API/Curl does not

Hive -
I have a Flask + React application that is running on Debian 11 via Nginx and Gunicorn. In development, everything works great ask it uses SQLAlchemy + SQLite to manage data queries.
In production, my .env file includes the connection details to the PostgreSQL database. After that is when it gets weird (at least for me, but this may be something that people commonly run into that my hours on Google just didn't turn up):
When I installed the app on production and set the .env file, I performed the flask db upgrade, and it wrote to the PostgreSQL database (confirmed tables exist).
When I ran the command line command to create an admin user in the new environment, it created my user in PostgreSQL on the users table with my admin flag.
When I go into flask shell I can import db from the app (which is just an instantiation of SQLAlchemy) and import User from the AUTH API. Once those are imported, I can run User.get.all() and it will return all users from the PostgreSQL table. I've even ensured there is a unique user in that table by manually creating it in the DB to validate that it doesn't get created in two systems.
When I use curl to hit the API to login in, it says that the users table is not found and references that it tried to query SQLite.
To summarize, I can not figure out why command line/shell interfaces correctly pull in the PostgreSQL connection but hitting the API falls back to SQLite. I'm not even sure where to start in debugging...even in the os_env call in the main app that says, "Pull from the env or fall back to development," I made the fall back = production.
All commands are executed in venv. Gunicorn is running within the same venv, and validated by tailing the logs that supervisor compiles for Gunicorn.
I am happy to provide any code that might be needed, but I am unsure what is and is not relevant. If it helps, the original base was built off of this boilerplate, and we have just expanded the API calls and models and defined a connection string to PostgreSQL in Production but left the SQLite connection string in development...the operation of the app works exactly the same: https://github.com/a-luna/flask-api-tutorial/tree/part-6
I finally found the answer.
When you launch Gunicorn, it ignores your .env file and any environment variables you may have set in the Shell. Even when your app specifically loads the .env file, Gunicorn still ignores it.
There are a variety of solutions but, since I was using Supervisor and also had a large number of variables to load, using the --env flag on Gunicorn was not an option.
Instead, add this to your Gunicorn file. Since I was using a virtualenv and had installed it via pip, my gunicorn command was running from ./project-root/venv/bin/gunicorn.
Modify that file as so:
At the top where your imports are, you will want to add:
import os
from dotenv import load_dotenv
Then, anywhere before you actually load the app (I put mine right after all of the imports), add this block of code where I have two environment files called .env and .flaskenv:
for env_file in ('.env', '.flaskenv'):
env = os.path.join(os.getcwd(), env_file)
if os.path.exists(env):
load_dotenv(env)

Why can my flask app no longer access my SQLite3 dabatase after being moved to a Ubuntu virtual machine?

I've made a flask project that worked without errors. Once I moved it to a ubuntu virtual machine for deployment, trying to deploy it using gunicorn/ nginx or apache2/ mod_wsgi raises the same error:
conn = sqlite3.connect('../db/bandwidthburst.db', check_same_thread=False)
sqlite3.OperationalError: unable to open database file
On similar questions the advice was to make sure the user has write permissions, so I've done 'chmod ugo+rwx' for my database, its parent folder and the parent folder to that aswell, which did not change the results.
Any help in fixing this would be much appreciated, thank you.
Now connects after changing the url to '/var/www/.../bandwidthburst.db'!
Going through fixing other errors now

Django deployed app/project can't write to a file

I am working on a Django based application whose location on my disk is home/user/Documents/project/application. Now this application takes in some values from the user and writes them into a file located in a folder which is under the project directory i.e home/user/Documents/project/folder/file. While running the development server using the command python manage.py runserver everything worked fine, however after deployment the application/views.py which accesses the file via open('folder/path','w') is not able to access it anymore, because by default it looks in var/www folder when deployed via apache2 server using mod_wsgi.
Now, I am not putting the folder into /var/www because it is not a good practise to put any python code there as it might become readable clients which is a major security threat. Please let me know, how can I point the deployed application to read and write to correct file.
The real solution is to install your data files in /srv/data/myapp or some such so that you can give the webserver user correct permissions to only those directories. Whether you choose to put your code in /var/www or not, is a separate question, but I would suggest putting at least your wsgi file there (and, of course, specifying your <DocumentRoot..> correctly.

Django project using wrong (old) database settings

recently I started a small Django project that I developed on a local machine using a SQLite3 database and the integrated development server. I now copied the whole project to a server running Debian.
Everything worked well as long as I kept using the SQLite3 database. Now I wanted to switch to a local MySQL database, so I changed the settings.py file in my project's root folder, created the database and added a user. I then ran syncdb and it created the needed tables without any problems.
Now I wanted to use the app, but I keep getting errors, because Django can not find the tables - neither the 'standard tables' like django_sessions nor my own tables - even though they are there (I checked the database).
The one thing that seems awkward about the DatabaseError pages I get is the following line:
Exception Location: /usr/local/lib/python2.6/dist-packages/django/db/backends/sqlite3/base.py in execute, line 234
It seems like Django is still using the SQLite3 backend even though I set it to use the MySQL backend.
The other thing that nearly makes me freak out is this: I deleted the data.sqlite file in my app's root folder using rm. But when I use my app, the file is being recreated!
Can anyone tell me where I went wrong?
When running in production with mod_wsgi or mod_python, new code isn't incorporated until you reload/restart the webserver.
If you are using apache with mod_wsgi, you can also touch the wsgi.py file to restart the python code only.
If running on apache run "sudo /etc/init.d/apache2 reload" everytime you make a change.

Categories

Resources