It appeared that I successfully deployed the application using this code
import sys, os
sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = "UR.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
However Django did not run properly because it could not get to the latest version of Django and ran an old version with Python 2.3. I installed the latest Django and Python 2.7 and I added those two lines and now I am getting an internal server error..
INTERP = '/home/username/opt/python-2.7.7/bin/python'
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
I am sure this is the right path to my Python 2.7 ... any help will be really appreciated
I finally solved it ,
I was deploying on dreamhost and their servers had django1.2 and python2.6, whenever i tried to make python2.7 and let it be the default passenger gets to python 2.6 and successfully executes python2.7 but for some reason it ran into django1.2 instead of the latest django i installed.
I installed python2.7 in /usr/ (using a sudoer account ofcourse) then i installed the new django into site packages and now I no longer need the lines that execute python2.7 as it is the default now along with django 1.7
I have also edited the passenger-wsgi file and this code worked very fine for me....
import sys, os
sys.path.append(os.path.join(os.getcwd(), 'projectname')) #I needed this
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
The only setback now is that the server is to slow in it's response but other than that everything went fine
And i found a great trick to get into the logs of django
ssh into your server
Execute python manage.py runserver
Open another ssh session at the same time
In the new ssh execute lynx localhost:8000
Then you will be able to see logs and debug information from django in the second ssh .
Related
I'm trying to deploy my first flask application and I'm running into some issues. I had my app working on my local machine with the build in flask development server, and all my dependencies were managed by pipenv. I uploaded my app to /var/www/directory_printer and ran pipenv install. Then I created a apache vhost file and pointed it to my .wsgi file:
WSGIScriptAlias / /var/www/directory_printer/directory.wsgi
In directory.wsgi, I import my app. At the beginning of my main app file, I import flask. When I try to access my app, I get a 500 error. In the apache error log I get:
ModuleNotFoundError: No module named 'flask'
If I start an interactive python shell in the directory_printer folder with the pipenv shell activated, I can import flask just fine.
I tried putting the path to my virtual env at the beginning of my directory.wsgi file:
#!/path/to/venv
but that doesn't seem to help. I'm sure I'm missing something simple, but I can't seem to see what it is. Any help would be appreciated. Thanks!
Often on Linux systems a home directory is not accessible to other users so the Apache user will not be able to read anything under the directory. Permissions can also be wrong on Python packages that have been installed making them inaccessible as well.
Ok, not sure if this is the correct answer, but it is now working for me.
First
create a .venv folder in the project root folder
then change permissions:
sudo chown www-data:www-data .venv
Then create a virtual environment and install your requirements from Pipfile as the user wsgi will run as:
sudo -su www-data python3 -m virtualenv -p python3 .venv pipenv install
This will install your virtual environment in the project folder. Check out this answer for more:
How to set PIPENV_VENV_IN_PROJECT on per-project basis
Then add this to your .wsgi folder:
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
Bottom of this page for more info:
https://flask.palletsprojects.com/en/2.0.x/deploying/mod_wsgi/
And now it works! Hopefully this will help somebody else out as well.
I am trying to port a django web application from some server to cloudera "applications" (data science workbench) and I was trying to make it work. I managed to do so with flask and fastapi applications, just the django framework is missing. My problem, when trying the base setup (https://docs.djangoproject.com/en/3.2/intro/tutorial01/), locally works like a charm but when I try to start up the server from an instance in cloudera, the server does not start and, more curiosuly, I get a weird output related to some package of the image I am spinning. (you can see I am bypassing the traditional runserver command on django because in the cloudera applications side I cannot run shells directly and also due to the fact that I would to tie it up with some environmental variable)
below the manage.py
import os
import sys
if __name__ == '__main__':
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.management import execute_from_command_line
port_to_pass = 8000
args = ['name', 'runserver', '127.0.0.1:%d' % port_to_pass]
execute_from_command_line(args)
weird "print" (the instance I am spinning has got the exact same package versions installed locally):
when I should be getting:
any idea what might the problem be?
You need an extra entry.py script:
!pip install django
!python manage.py runserver 8000
I've built a Django project that works, even after I freeze it using Cx_Freeze and Py2exe.
Now I'm trying to set up the project for distribution, which requires a real webserver. I'm going for Gunicorn (will add Nginx once it works). I managed to run the Gunicorn server properly through the command line using :
gunicorn wsgi:application
However, I need to be able to run the server from my Python script, as the server is ment to be localhost. Gunicorn used to be shipped with a command 'run_gunicorn' designed for Django, but this command is now deprecated.
I tried understanding the following method :
How to use Flask-Script and Gunicorn
But I can't figure out how to make it work with Django.
The following doesn't work:
from django.core.wsgi import get_wsgi_application
from gunicorn.app.base import Application
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
application = get_wsgi_application()
Application().run(application)
Could someone tell me how to start the gunicorn server from my Python script ?
This does not work for me
$> cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"
django 1.7rc3
celery 3.1.13
python 2.7
I attempt to run
celery worker -A <project_name>
and I get
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
The runserver command works fine so I don't think it has to do with my settings?
python manage.py runserver 0.0.0.0:8080
I've double checked celery.py and confirm it has the correct values for the following lines:
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
app = Celery('proj')
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
Is there something else i should be doing?
Django 1.7 now requires a different initialization for standalone scripts. When running outside the manage.py context, you now need to include:
import django
django.setup()
Try adding it before the app = Celery('proj') in your script.
I found out that the reason this was failing is because i had this in one of my tasks.py
CURRENT_DOMAIN = Site.objects.get_current().domain
I got side steped this for now with
CURRENT_DOMAIN = lambda: Site.objects.get_current().domain
currently waiting to see if anyone on github cares to offer a better recommendation.
https://github.com/celery/celery/issues/2227
will update if i get one. If not will probably just do a helper function that lazy returns the value i want.
update
at the suggestion of celery author, I've refactored my code so I don't make that call at the module level.
He's also addressed the issue by making sure django.setup() is called before importing task modules
https://github.com/celery/celery/issues/2227
I'm seeing the same issue however only with:
CELERYBEAT_SCHEDULER='djcelery.schedulers.DatabaseScheduler'
Do you have this enabled? If I run it with the default celery scheduler it loads fine. But can't get it to load with the Django scheduler.
I'm developing a flask.app that uses web socket functionality and installed flask-socket to provide that. So the flask-socket developer recommends gunicorn as web server. My question is now how to connect the remove debugger of pycharm with gunicorn to intercept the execution with breakpoints.
Settings > Project Settings > Python Debugger
There's an option in there to enable "gevent compatible debugging".
Then, go into your debugger settings (shortcut is through the toolbar, click the dropdown near the play/debug icons and select "Edit Configurations"
Set the "Script" to your virtualenv's isntallation of gunicorn, something like:
/Users/iandouglas/.virtualenvs/defaultenv/bin/gunicorn
Set the "Script Parameters" to something like
-b 192.168.1.1:9000 app:yourappname (assuming your primary starting script is called app.py and you're refering to it as 'yourappname'
the "Working directory" will be automatically set otherwise set it to wherever your code lives: /Users/iandouglas/PycharmProjects/MyExampleApp
I have a separate config file for my gunicorn settings, which specifies a host/port but I still had to specify the -b 0.0.0.0:5001 parameter to force gunicorn to bind to all IPs on my machine on port 5001.
p.s.
One important step is to add this envvar as pointed out here
PYDEVD_USE_CYTHON=NO
My case for PyCharm 2018.1.3 Professional:
Go to run/debug configurations
creating-and-editing-run-debug-configurations
Choose new "Python" config
Script path: your_path_to_/venv/bin/gunicorn
Parameters(for my case): -b :5001 --access-logfile - --error-logfile - "run:create_application()"
Python interpreter: your venv python version for project
Working directory: path to your project
Save and press DEBUG (Shift+F9)
be happy!
I was trying to debug on Pycharm 2020.2.1 and the break points weren't correctly working even though the Gevent compatible debugging was enabled. It turned out that I had to disable Cython for my run configuration via setting the environment variable as described here to make it work.
PYDEVD_USE_CYTHON=NO
edit your flask launch python file
$ vim manage.py
remove debug option setting
from web import app
import sys
if __name__ == '__main__':
# app.run(host='0.0.0.0', port=app.config["PORT"], debug=app.config["DEBUG"])
app.run(host='0.0.0.0', port=9998, debug=False)