uwsgi + django import error - python

I have a problem when I use django with uwsgi with the pythonpath.
I have a django project named 'project' which is the /sites/django/ directory
So to start uwsgi i use this command :
/opt/uwsgi/uwsgi -s 127.0.0.1:9001 -C -M 4 -t 30 -A 4 -p 4 -d /var/log/uwsgi.log --pythonpath '/sites/django/project/' --module wsgi
If I am in the /sites/django/project' it works.
If I start a python shell and I write :
import sys
sys.path.append('/sites/django/project/')
import wsgi
It works too.
But when i launch the uwsgi command outside the /sites/django/project/ I have the error :
ImportError: No module named wsgi
So I don't know why I have the ImportError : it works in the shell.
If anyone has an idea,
Thanks.
Well, i find the answer, it seems it's a bug and i must add the "-i" option (single interpreter mode ) in my command .

The documentation seems to suggest two possibilities.
First, remove the single quotes from the python path argument. Second, the examples on the linked page have --python-path instead of --pythonpath (even though the index at the top of the page says otherwise). Worth trying.

if you're using a virtualenv, you need to pass in the -H flag ( http://projects.unbit.it/uwsgi/wiki/VirtualEnv )

Related

Gunicorn ModuleNotFoundError

I'm reading a book about TDD and Django and there's a deployment part. I have a problem trying to run gunicorn with the following command:
/root/sites/django_blog/virtualenv/bin/gunicorn --bind unix:/tmp/django_blog.socket django_blog.wsgi:application
It fails with the following error:
ModuleNotFoundError: No module named 'django_blog'
But when I activate my virtualenv and instead of writing the full pass to gunicorn I just go with:
gunicorn --bind unix:/tmp/django_blog.socket django_blog.wsgi:application
And everything works perfectly! The problem is I still need to run it the first way, because I wil use it in the nginx service file. I wrote about this error and tried a couple of solutions but they didn't work for me. I guess I have to do something with environment variables but I don't know what exactly.
You can specify a directory to gunicorn to switch to before the apps are loaded.
Simply add --chdir /path/to/directory to the launch.
In your case this might look as follows:
/root/sites/django_blog/virtualenv/bin/gunicorn --chdir /root/sites/django_blog/source --bind unix:/tmp/django_blog.socket django_blog.wsgi:application
Here is the link to the specific gunicorn settings documentation.
Hope that helps and happy coding!

docker and python using symfony process

I am using Laradock and want to be able to run a python script from my laravel app using Symfony Process. From inside the root on my container I can run "python3 script_name.py arg1" and it runs just fine. pip list shows all modules needed. When I run it from inside Laravel, it tells me:
"import pymysql ImportError: No module named 'pymysql'"
I have used a non-docker Laravel app to do this just fine, using:
$script = storage_path().'/app/script.py';
$process = new Process('python3 '. $script." ".session('division'));
What am I missing?
On *nix make sure that PYTHONPATH is configured correctly for all users or try to set full path to python3.
How to check
At first your php user
php -r "print shell_exec( 'whoami' );" // somebody
When run
su somebody python3 script_name.py arg1

How to create mysite.wsgi file?

I am following this tutorial http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html to setup django with nginx and uwsgi. But I am confused about this line:
uwsgi --http :8000 --module mysite.wsgi
In the tutorial there's nothing about mysite.wsgi file. What should be the content of this file?
Actually, that's not a filename.
That's a python module path.
The relevant file is actually mysite/wsgi.py (but to import it in a python interpreter, you'd have to import mysite.wsgi, hence the name used in command line).

openshift environment variable not used in virtualenv

I can't understand why my custom env variables aren't read during app execution, but if I type env command or use python shell they looks ok.
For example:
if I ssh to my app and type:
$ env | grep AMAZON
AMAZON_KEY=...
AMAZON_SECRET=...
and this is correct, it's my custom variable. But if I exec the application I've go:
$ KeyError: 'AMAZON_KEY'
Thanks!
try stopping and starting your application with the rhc command (not a restart) and see if that helps.

Deploying CherryPy (daemon)

I've followed the basic CherryPy tutorial (http://www.cherrypy.org/wiki/CherryPyTutorial). One thing not discussed is deployment.
How can I launch a CherryPy app as a daemon and "forget about it"? What happens if the server reboots?
Is there a standard recipe? Maybe something that will create a service script (/etc/init.d/cherrypy...)
Thanks!
Daemonizer can be pretty simple to use:
# this works for cherrypy 3.1.2 on Ubuntu 10.04
from cherrypy.process.plugins import Daemonizer
# before mounting anything
Daemonizer(cherrypy.engine).subscribe()
cherrypy.tree.mount(MyDaemonApp, "/")
cherrypy.engine.start()
cherrypy.engine.block()
There is a decent HOWTO for SysV style here.
To summarize:
Create a file named for your application in /etc/init.d that calls /bin/sh
sudo vim /etc/init.d/MyDaemonApp
#!/bin/sh
echo "Invoking MyDaemonApp";
/path/to/MyDaemonApp
echo "Started MyDaemonApp. Tremble, Ye Mighty."
Make it executable
sudo chmod +x /etc/init.d/MyDaemonApp
Run update-rc.d to create our proper links in the proper runtime dir.
sudo update-rc.d MyDaemonApp defaults 80
sudo /etc/init.d/MyDaemonApp
There is a Daemonizer plugin for CherryPy included by default which is useful for getting it to start but by far the easiest way for simple cases is to use the cherryd script:
> cherryd -h
Usage: cherryd [options]
Options:
-h, --help show this help message and exit
-c CONFIG, --config=CONFIG
specify config file(s)
-d run the server as a daemon
-e ENVIRONMENT, --environment=ENVIRONMENT
apply the given config environment
-f start a fastcgi server instead of the default HTTP
server
-s start a scgi server instead of the default HTTP server
-i IMPORTS, --import=IMPORTS
specify modules to import
-p PIDFILE, --pidfile=PIDFILE
store the process id in the given file
As far as an init.d script goes I think there are examples that can be Googled.
And the cherryd is found in your:
virtualenv/lib/python2.7/site-packages/cherrypy/cherryd
or in: https://bitbucket.org/cherrypy/cherrypy/src/default/cherrypy/cherryd
I wrote a tutorial/project skeleton, cherrypy-webapp-skeleton, which goal was to fill the gaps for deploying a real-world CherryPy application on Debian* for a web-developer. It features extended cherryd for daemon privilege drop. There's also a number of important script and config files for init.d, nginx, monit, logrotate. The tutorial part describes how to put things together and eventually forget about it. The skeleton part proposes a way of possible arrangement of CherryPy webapp project assets.
* It was written for Squeeze but practically it should be same for Wheezy.
Info on Daemonizer options
When using Daemonizer, the docs don't state the options, e.g. how to redirect stdout or stderr. From the source of the Daemonizer class you can find the options. As a reference take this example from my project:
# run server as a daemon
d = Daemonizer(cherrypy.engine,
stdout='/home/pi/Gate/log/gate_access.log',
stderr='/home/pi/Gate/log/gate_error.log')
d.subscribe()

Categories