How can I import my own modules on webserver? - python

I am developing a website and want to put some files on the server. The problem is that I can only import python modules ( e.g. "import os"), but somewhy, can not import my own modules:
test1.py:
import test2
test2.py:
print ("Content-type:text/html\n\n")
print ("<html>")
print ("<head>")
print ("<title>Error</title>")
print ("</head>")
print ("<body>")
print (" hello world 2")
print ("</body>")
print ("</html>")
If I click www.mywebsite.com/test2.py, then I recieve "hello world" on the screen.
However, if I click www.mywebsite.com/test1.py, I get "500 internal server error." I found out that it is some problem with not being able to import my modules.
p.s. since I am on a shared server, I can not changes sys path etc....
Here is a trace I got in errors.log:
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] mod_python (pid=17816, interpreter='delekulator.co.il', phase='PythonHandler', handler='mod_python.cgihandler'): Application error
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] ServerName: 'delekulator.co.il'
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] DocumentRoot: '/var/www/vhosts/mywebsite.com/httpdocs'
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] URI: '/test1.py'
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] Location: None
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] Directory: '/var/www/vhosts/mywebsite.com/httpdocs/'
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] Filename: '/var/www/vhosts/mywebsite.com/httpdocs/test1.py'
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] PathInfo: ''
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] Traceback (most recent call last):
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch\n default=default_handler, arg=req, silent=hlist.silent)
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1229, in _process_target\n result = _execute_target(config, req, object, arg)
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] File "/usr/lib/python2.6/dist-packages/mod_python/importer.py", line 1128, in _execute_target\n result = object(arg)
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] File "/usr/lib/python2.6/dist-packages/mod_python/cgihandler.py", line 96, in handler\n imp.load_module(module_name, fd, path, desc)
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] File "/var/www/vhosts/mywebsite.com/httpdocs/test1.py", line 1, in <module>\n import test2
[Thu Jun 11 00:18:09 2015] [error] [client 85.65.174.249] ImportError: No module named test2
In the last line, you can see that the server writes "ImportError: No module named test2"
How can I fix it?

From your description, your program did not found your own module, because you did not give your module's path. Now there are two methods to solve it:
Assume your module's path is: /srv/modules/xxxx.py
First you should import your module in your program: import xxxx
command line:Before you run your program with command line, give the path first. command:
export PYTHONPATH=/srv/modules.Then run your program.
add two lines of codes in your program:
import sys
sys.path.append("/srv/modules")
Use any one way above, you should run your program success.I hope this can help you.

Modules should be in the Python path or in the same directory where the executable is.
Then you do like usually
import test2
test2.some_function() # or whatever.

Related

Python: Flask import failing

I'm trying to deploy a Flask app on Apache using mod_wsgi. Im using a virtual environment. But the below line is failing.
from flask import Flask
Error:
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] mod_wsgi (pid=6937): Target WSGI script '/var/www/sample_rest/sample_rest.wsgi' cannot be loaded as Python module.
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] mod_wsgi (pid=6937): Exception occurred processing WSGI script '/var/www/sample_rest/sample_rest.wsgi'.
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] Traceback (most recent call last):
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/sample_rest.wsgi", line 5, in <module>
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] from sample_rest import sample_rest as application
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/sample_rest.py", line 2, in <module>
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] from flask import Flask, request, jsonify, make_response
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/lib/python3.6/site-packages/flask/__init__.py", line 16, in <module>
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] from werkzeug.exceptions import abort
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/lib/python3.6/site-packages/werkzeug/__init__.py", line 15, in <module>
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] from .serving import run_simple
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/lib/python3.6/site-packages/werkzeug/serving.py", line 51, in <module>
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] from .exceptions import InternalServerError
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/lib/python3.6/site-packages/werkzeug/exceptions.py", line 68, in <module>
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] from .utils import escape
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/lib/python3.6/site-packages/werkzeug/utils.py", line 189
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] "area",
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] ^
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] SyntaxError: invalid syntax
As you can see the error is within the code of flask itself. I'm using Python-3.6.2 which is supported by flask. The source code of flask in which it's showing error is also correct. What could be the issue ?
Sorry for posting this rather then commenting:
Try to install Flask 1.1.1 which worked for me:
pip install Flask==1.1.1

ImportError: No module named types

I am getting below error when I start the httpd server and access the app url -
[Tue Feb 21 22:01:51 2017] [error] [client 10.209.33.139] mod_wsgi (pid=15425): Target WSGI script '/scratch/gdudwadk/python-projects/newblog/app/wsgi.py' cannot be loaded as Python module.
[Tue Feb 21 22:01:51 2017] [error] [client 10.209.33.139] mod_wsgi (pid=15425): Exception occurred processing WSGI script '/scratch/gdudwadk/python-projects/newblog/app/wsgi.py'.
[Tue Feb 21 22:01:51 2017] [error] Traceback (most recent call last):
[Tue Feb 21 22:01:51 2017] [error] File "/scratch/gdudwadk/python-projects/newblog/app/wsgi.py", line 7, in <module>
[Tue Feb 21 22:01:51 2017] [error] from app import app as application
[Tue Feb 21 22:01:51 2017] [error] File "/scratch/gdudwadk/python-projects/newblog/app/app.py", line 1, in <module>
[Tue Feb 21 22:01:51 2017] [error] from flask import Flask
[Tue Feb 21 22:01:51 2017] [error] File "/scratch/gdudwadk/python-projects/newblog/venv/lib/python2.6/site-packages/flask/__init__.py", line 17, in <module>
[Tue Feb 21 22:01:51 2017] [error] from werkzeug.exceptions import abort
[Tue Feb 21 22:01:51 2017] [error] File "/scratch/gdudwadk/python-projects/newblog/venv/lib/python2.6/site-packages/werkzeug/__init__.py", line 17, in <module>
[Tue Feb 21 22:01:51 2017] [error] from types import ModuleType
[Tue Feb 21 22:01:51 2017] [error] ImportError: No module named types
This started happening after my machine was replaced with new one. Previously app was working fine.
I found one reference to python2.7 in /etc/httpd/conf.d/wsgi.conf and that's what was causing the issue. After removing that, the app was up and running.. Thanks!

odoo mod_wsgi lxml.etree undefined symbol: PyUnicodeUCS2_DecodeLatin1

I try to configure mod_wsgi to access odoo thanks my domain name.
I followed this tutorial.
But when I go to my site I get this error :
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] mod_wsgi (pid=30445): Target WSGI script '/opt/odoo-v9-prod/openerp-wsgi.py' cannot be loaded as Python module.
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] mod_wsgi (pid=30445): Exception occurred processing WSGI script '/opt/odoo-v9-prod/openerp-wsgi.py'.
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] Traceback (most recent call last):
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] File "/opt/odoo-v9-prod/openerp-wsgi.py", line 15, in <module>
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] import openerp
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] File "/opt/odoo-v9-prod/openerp/__init__.py", line 58, in <module>
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] import modules
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] File "/opt/odoo-v9-prod/openerp/modules/__init__.py", line 8, in <module>
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] from . import db, graph, loading, migration, module, registry
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] File "/opt/odoo-v9-prod/openerp/modules/graph.py", line 13, in <module>
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] import openerp.osv as osv
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] File "/opt/odoo-v9-prod/openerp/osv/__init__.py", line 4, in <module>
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] import osv
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] File "/opt/odoo-v9-prod/openerp/osv/osv.py", line 4, in <module>
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] from ..exceptions import except_orm
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] File "/opt/odoo-v9-prod/openerp/exceptions.py", line 15, in <module>
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] from lxml import etree
[Thu Nov 03 16:00:57 2016] [error] [client 176.189.147.89] ImportError: /usr/local/lib/python2.7.9/lib/python2.7/site-packages/lxml/etree.so: undefined symbol: PyUnicodeUCS2_DecodeLatin1
I test my python encodage and it is UCS2.
I reinstalled lxml.
Your mod_wsgi is compiled for a different Python installation than the Python virtual environment you are trying to force it to use, or the Python shared library it is finding. Worse is that what ever it is using was compiled with different width for Unicode character support. You need to uninstall mod_wsgi and reinstall mod_wsgi from source code and compile it against the specific Python version you do want to use, forcing it to use the correct shared library if necessary. You may also want to consider reinstalling your custom Python version to use a configuration which aligns with what is generally regarded as best practice for the platform. Linux system usually use 4 byte Unicode, not 2.
Some stuff for you to read:
http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html#python-shared-library
http://blog.dscpl.com.au/2015/06/installing-custom-python-version-into.html
Ignore the fact that the latter talks about Docker, all still relevant to normal Python install as well.

Run with apache + mod_wsgi, Import error: cannot enable executable stack as shared object requires: Permission denied

I'm using apache with Flask and mod_wsgi.
When I import scipy module on my Flask script I got this error.
[Thu Oct 15 18:15:45 2015] [error] File "/usr/local/lib/python2.7/site-packages/pymatgen/electronic_structure/plotter.py", line 671, in get_plot3
[Thu Oct 15 18:15:45 2015] [error] import scipy.interpolate as scint
[Thu Oct 15 18:15:45 2015] [error] File "/usr/local/lib/python2.7/site-packages/scipy/interpolate/__init__.py", line 145, in <module>
[Thu Oct 15 18:15:45 2015] [error] from .interpolate import *
[Thu Oct 15 18:15:45 2015] [error] File "/usr/local/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 16, in <module>
[Thu Oct 15 18:15:45 2015] [error] import scipy.special as spec
[Thu Oct 15 18:15:45 2015] [error] File "/usr/local/lib/python2.7/site-packages/scipy/special/__init__.py", line 608, in <module>
[Thu Oct 15 18:15:45 2015] [error] from ._ellip_harm import ellip_harm, ellip_harm_2, ellip_normal
[Thu Oct 15 18:15:45 2015] [error] File "/usr/local/lib/python2.7/site-packages/scipy/special/_ellip_harm.py", line 7, in <module>
[Thu Oct 15 18:15:45 2015] [error] from ._ellip_harm_2 import _ellipsoid, _ellipsoid_norm
[Thu Oct 15 18:15:45 2015] [error] File "scipy/special/_ellip_harm_2.pyx", line 2, in init scipy.special._ellip_harm_2 (scipy/special/_ellip_harm_2.c:7444)
[Thu Oct 15 18:15:45 2015] [error] File "/usr/local/lib/python2.7/ctypes/__init__.py", line 10, in <module>
[Thu Oct 15 18:15:45 2015] [error] from _ctypes import Union, Structure, Array
[Thu Oct 15 18:15:45 2015] [error] ImportError: /usr/local/lib/python2.7/lib-dynload/_ctypes.so: cannot enable executable stack as shared object requires: Permission denied
How can I fixed this?
I suppose you are using a SELinux enabled distribution like CentOS. When your app is behind Apache using mod_wsgi, you have to set the label httpd_sys_script_exec_t to the shared object files before your app can access them.
In the following example I will assume that your application is in /var/www/myapp directory with Python 2.7 virtualenv in /var/www/myapp/venv and Numpy and Scipy is installed in that virtualenv as well.
You can add permanently the new SELinux policy using semanage:
semanage fcontext -a -t httpd_sys_script_exec_t "/var/www/myapp/venv/lib/python2.7/site-packages(/.*)?"
And then you can activate them with restorecon:
restorecon -R /var/www/myapp/venv/lib/python2.7/site-packages/
If you install a new package in the virtualenv which has a shared object file, then you have to run the restorecon command again (or reboot).

apache python error

I am getting this in my error log file. Ive been searching for asolution but have found nothing. I am trying to deploy django with mod_wsgi.
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] ImportError: /usr/lib/python2.6/lib-dynload/_functools.so: wrong ELF class: ELFCLASS32
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] mod_wsgi (pid=20219): Target WSGI script '/home/dbs/public_html/test.wsgi' cannot be loaded as Python module.
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] mod_wsgi (pid=20219): Exception occurred processing WSGI script '/home/dbs/public_html/test.wsgi'.
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] Traceback (most recent call last):
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] File "/home/dbs/public_html/test.wsgi", line 6, in <module>
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] import django.core.handlers.wsgi
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] File "/home/dbs/sites/domain.com/django/core/handlers/wsgi.py", line 2, in <module>
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] from threading import Lock
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] File "/usr/lib/python2.6/threading.py", line 13, in <module>
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] from functools import wraps
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] File "/usr/lib/python2.6/functools.py", line 10, in <module>
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] from _functools import partial, reduce
You have the wrong version of the functional module installed. Either install the correct binary version, or use the pure-python version.

Categories