2017-08-08 10 views
2

ich ein einfaches Python-Programm geschrieben:gunicorn korrumpiert sys.path

# /tmp/src/Code.py 
import sys 
print sys.path 

# /tmp/src/Main.py 
import Code 

Wenn ich es mit python src/Main.py laufen, funktioniert es wie erwartet:

max% cd /tmp 
max% setenv PYTHONPATH src 
max% python src/Main.py 
['/tmp/src', 
'/tmp/src', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-x86_64-linux-gnu', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages/PILcompat', 
'/usr/lib/python2.7/dist-packages/gtk-2.0', 
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client'] 

Und sicher sys.path machen arbeitet richtig, erstelle ich eine Datei im Arbeitsverzeichnis:

# /tmp/Code.py 
print "I never said to search CWD!!! Your Python is broken." 
import sys 
print sys.path 

Und das Ergebnis ist das gleiche wie oben, wie exp erreicht.

Allerdings, wenn ich in gunicorn laufen erhalte ich:

max% gunicorn Main:app 
2017-08-08 10:30:53 [26913] [INFO] Starting gunicorn 17.5 
2017-08-08 10:30:53 [26913] [INFO] Listening at: http://127.0.0.1:8000 (26913) 
2017-08-08 10:30:53 [26913] [INFO] Using worker: sync 
2017-08-08 10:30:53 [26918] [INFO] Booting worker with pid: 26918 
I never said to search CWD!!! Your Python is broken. 
['/tmp', 
'/usr/bin', 
'/tmp/src', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-x86_64-linux-gnu', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages/PILcompat', 
'/usr/lib/python2.7/dist-packages/gtk-2.0', 
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client'] 

Es scheint, dass gunicorn zufällig PWD zum sys.path hinzuzufügen entschieden. Es gibt nichts in der Gunicorn-Manpage darüber.

Python-Konfiguration:

Flask==0.10.1 
Jinja2==2.7.2 
MarkupSafe==0.18 
PAM==0.4.2 
Pillow==2.3.0 
Twisted-Core==13.2.0 
Twisted-Web==13.2.0 
Werkzeug==0.9.4 
adium-theme-ubuntu==0.3.4 
apt-xapian-index==0.45 
argparse==1.2.1 
blinker==1.3 
chardet==2.0.1 
colorama==0.2.5 
command-not-found==0.3 
debtagshw==0.1 
defer==1.0.6 
dirspec==13.10 
duplicity==0.6.23 
gevent==1.0 
greenlet==0.4.2 
gunicorn==17.5 
html5lib==0.999 
httplib2==0.8 
itsdangerous==0.22 
lockfile==0.8 
lxml==3.3.3 
oauthlib==0.6.1 
oneconf==0.3.7.14.04.1 
pexpect==3.1 
piston-mini-client==0.7.5 
pyOpenSSL==0.13 
pycrypto==2.6.1 
pycups==1.9.66 
pygobject==3.12.0 
pyinotify==0.9.4 
pyserial==2.6 
pysmbc==1.0.14.1 
python-apt==0.9.3.5ubuntu2 
python-debian==0.1.21-nmu2ubuntu2 
pyxdg==0.25 
reportlab==3.0 
requests==2.2.1 
sessioninstaller==0.0.0 
simplejson==3.3.1 
six==1.5.2 
software-center-aptd-plugins==0.0.0 
ssh-import-id==3.21 
system-service==0.1.6 
unity-lens-photos==1.0 
urllib3==1.7.1 
wheel==0.24.0 
wsgiref==0.1.2 
xdiagnose==3.6.3build2 
zope.interface==4.0.5 

Ich weiß, ich PWD aus dem sys.path realpath(p) == realpath('.') durch die Suche entfernen konnte, aber manchmal wollen wir PWD in der PYTHONPATH. Daher ist eine sorgfältigere Lösung erforderlich. Im Idealfall würden wir versuchen herauszufinden, welche Software den Fehler verursacht. Es könnte einfach sein, dass die Gunicorn-Manpage unvollständig ist?

+1

Ich fand 'gunicorn --pythonpath src' funktioniert besser als' setenv PYTHONPATH src'. Ich wünschte, Gunicorn hätte eine Dokumentation. –

+0

Es scheint, dass Gunicorn '--pythonpath' vor PWD und dann 'PYTHONPATH' vorlegt. Daher sollte es immer eine allgemeine Lösung sein, Gunicorn mit '--pythonpath $ PYTHONPATH 'auszuführen. Dies sollte wirklich zur Manpage hinzugefügt werden. –

+0

beschreiben, wie Sie Ihr Problem als Antwort auf die Frage gelöst haben, dann genehmigen Sie es. – marcusshep

Antwort

1

bearbeiten /usr/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py und ändern Sie die folgende Zeile ein:

sys.path.insert(0, cwd) 

zu:

sys.path.append(cwd) 

Dann --pythonpath funktioniert wie erwartet.

+0

Wie erstellen wir ein Standard-Python-Paket, das die oben gezeigte Bearbeitung ausführt? Nennen wir unser Paket 'gunicorn_pythonpath_enabler' oder somesuch. –

+0

Eine Pull-Anfrage an Gunicorn zu machen wäre effizienter, als ein Patch in irgendeiner lib zu packen. – bfontaine