【发布时间】:2015-06-10 21:00:35
【问题描述】:
我正在开发一个网站,并希望将一些文件放在服务器上。问题是我只能导入 python 模块(例如“import os”),但不知何故,不能导入我自己的模块:
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>")
如果我点击 www.mywebsite.com/test2.py,然后我会在屏幕上收到“hello world”。 但是,如果我单击 www.mywebsite.com/test1.py,我会收到“500 内部服务器错误”。我发现无法导入我的模块是一些问题。
附言因为我在共享服务器上,所以我无法更改系统路径等......
这是我在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
在最后一行可以看到服务器写“ImportError: No module named test2”
我该如何解决?
【问题讨论】:
-
查看您的错误日志正在打印什么...在共享主机上,您需要搜索
error.log(在共享主机上不应限制您设置自己的路径变量的能力) -
也许您需要在当前目录中有一个
__init__.py文件,以便将其标记为一个包? -
看起来您正在使用 CGI?您应该使用 uwsgi、fast_cgi、mod_wsgi 或 mod_proxy 进行部署。
-
@Yura CGI 已经过时、过时且已弃用。几乎没有人 不再以这种方式部署了;肯定不在生产中。
-
和 test2.py 在同一个文件夹中?/var/www/vhosts/mywebsite.com/httpdocs/ ?您可以尝试将
sys.path.append(".")添加到 test1.py 的顶部,以尝试确保路径中包含本地目录(我认为总是如此,但是 meh)
标签: python python-3.x