Gunicorn's pythonpath error - python

When starting python web application using gunicorn command, like gunicorn -b 127.0.0.1:8080.
It prompts cannot find packages like numpy which ran default Python interpreter. I added pythonpath to gunicorn command, but it doesn't work. BTW, I created a virtual env in the application folder.
Hope someone could provide a solution.

The problem is you are using default python to execute gunicorn. Either you activate your virtualenv and then execute the gunicorn -b 127.0.0.1:8080 or else you can use the following command
/ABSOLUTE_PATH/vitual_python_env/bin/gunicorn -b 127.0.0.1:8080

Related

Gunicorn - exits the terminal automatically immediatly after running venv

When I run my gunicorn command in python venv, the terminal gets closed abruptly, without giving any message or error.
It is a sample python falcon api. I am able to run the api if I am not using the venv.
The command used for running gunicorn:
exec bin/gunicorn sample_api:api -b 8080
Is there something to be taken care when we are running gunicorn app using venv.

Run DJango server from outside of virtual environment by using script

I have created DJango Project inside virtual environment.
Let's say virtual environment DJangoProject project name is Mysite
I am tired of running ./manage.py runserver everytime so that I wanted to automate running server when I was logged into ubuntu.I tried many ways but was always failing to access the path of virtual environment. So help me to run manage.py runserver from outside of virtual environment using bash or shell scripting.
any help can be appreciate!
What are looking for is something like Gunicorn, because runserver is only for development purposes. This is detailed tutorial for you to use
Link
You can't "run manage.py runserver from outside of virtual environment" if your project relies on a virtual environment... But nothing prevents you from writing a wrapper bash script that cd into your project's root, activate the virtualenv and launch the dev server.
This being said, I really don't see the point - and I even see a couple reasons to not do so, the first one being that you definitly want to keep the terminal where you run your dev server from opened so you can read all the logs in real time.
That's easy in Ubuntu you just need to create a shell script and run it from it's folder in the terminal as : ./shellscriptname.sh
but for that first you need to create the shell script by writing the same commands used on terminal to run the django server.
Example:
echo Hello Sangeeth
echo Lets start the app
cd myapp
cd venv
source bin/activate
cd ..
cd myapp
python manage.py runserver
even though this method helps you run the app , you wont be seeing the actual folder access and virtual environment on the terminal.

Running manage.py command through Gunicorn

I followed this tutorial to set up Gunicorn to run Django on a VPS, this is working perfectly fine and the web server is running on Nginx.
I created a separate manage.py command that I want to run Async using a worker, I am unsure how to integrate this through Gunicorn.
This is a follow up to Run code on first Django start, where the recommendation was to create a separate manage.py command and then run it as a separate worker process through Gunicorn.
Gunicorn's purpose here is to serve the Django project using WSGI, it doesn't use manage.py at all. You should call anything related to manage.py directly:
$ cd <projectdir>
$ source myprojectenv/bin/activate
$ python manage.py <your command here>
For setting it as a worker, you can either set a cron job that points the python binary in the virtualenv or you can consider making a Celery setup with the process management tool (supervisord, docker etc) of your choice.

How to make sudo execute in current python virtual environment?

I have a django website setup and configured in a python virtual environment (venv) on Ubuntu and all is working fine. Now in order to to run my server on port80 I need to use "sudo" which does not execute in the context of the virtual environment, raising errors (i.e no module named django ...)
Is there a way to get "sudo" to execute in the context of the python virtual environment?!
No, you don't need to do this. You shouldn't be trying to run the development server on port 80; if you're setting up a production environment, use a proper server.
As #DanielRoseman said you should not be using the Django development server in production.
But if you need to run the development server on port 80 you have to reference the use the virtual environment python executable directly.
sudo ../bin/python manage.py runserver localhost:80
This should be the solution.
Even tho I really don't recomment doing this. If you need sudo within Python you are probably on a wrong way.
this did the trick:
$ sudo -- sh -c './venv-bin-path/activate; gunicorn <params> -b 0.0.0.0:80'

uWSGI doesn't recognize --http nor --wsgi-file options

I'm trying to setup a webserver with nginx, uwsgi and django.
nginx works and it serves the files in /srv just like I intended to. However I can't get uwsgi to work properly. My biggest problem is that I used this tutorial and they don't seem to use the version of uwsgi that I use.
At one point they call uwsgi --http :8000 --wsgi-file test.py, but my uwsgi has neither the --http long option, nor --wsgi-file long option. uwsgi --version prints 2.0.7-debian.
What can I call instead of --wsgi-file?
UPDATE:
This works fine, i.e. I can see my website on localhost on port 8000:
python3 manage.py runserver 0.0.0.0:8000
However this does not:
uwsgi --http :8000 --module django_test.wsgi
It will complain, that it doesn't know the long option --http as well as the long option --module. It all seems to boil down to uwsgi, which on my machine just doesn't behave as it does everywhere else.
uWSGI installed from debian packages doesn't have any built-in plugins, and for that parameters to work, you will need:
http plugin for --http parameter
python plugin for --module (or any wsgi related) parameter
You can fix this in 2 ways: installing all required plugins (uwsgi-plugins-all package will install all available plugins on debian) and loading them explicitly (--plugin http and --plugin python) or by replacing uWSGI by fully featured one (sudo pip install uwsgi will do that). Second solution will tie your uWSGI up to certain python version, for first one, you can just load another python plugin (python3 for example).

Categories