【问题标题】:How can I import my own modules on webserver?如何在网络服务器上导入我自己的模块?
【发布时间】: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


【解决方案1】:

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

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

  1. 命令行:在使用命令行运行程序之前,请先给出路径。命令: export PYTHONPATH=/srv/modules。然后运行你的程序。

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

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

使用上述任何一种方式,您应该可以成功运行您的程序。希望对您有所帮助。

【讨论】:

    【解决方案2】:

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

    那你通常喜欢

    import test2
    
    test2.some_function() # or whatever.
    

    【讨论】:

    猜你喜欢
    • 2014-08-12
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多