如何在Web服务器上导入自己的模块?

时间:2015-06-10 21:00:36

标签: python python-3.x

我正在开发一个网站,并希望在服务器上放置一些文件。问题是我只能导入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内部服务器错误”。我发现无法导入我的模块存在一些问题。

P.S。因为我在共享服务器上,所以我无法更改sys路径等....

这是我在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”

我该如何解决?

2 个答案:

答案 0 :(得分:0)

模块应位于Python路径中或可执行文件所在的同一目录中。

然后你喜欢

import test2

test2.some_function() # or whatever.

答案 1 :(得分:0)

根据您的描述,您的程序没有找到您自己的模块,因为您没有提供模块的路径。现在有两种方法可以解决它:

假设您的模块路径为:/srv/modules/xxxx.py 首先,您应该在程序中导入模块:import xxxx

  1. 命令行:在使用命令行运行程序之前,首先提供路径。命令: export PYTHONPATH=/srv/modules。然后运行您的计划。

  2. 在程序中添加两行代码:

    import sys sys.path.append("/srv/modules")

  3. 使用上述任何一种方法,您应该成功运行程序。我希望这可以帮助您。

相关问题