[英]How can I import my own modules on webserver?
I am developing a website and want to put some files on the server. 我正在开发一个网站,并希望将一些文件放在服务器上。 The problem is that I can only import python modules ( eg "import os"), but somewhy, can not import my own modules:
问题是我只能导入python模块(例如“ import os”),但是为什么不能导入我自己的模块:
test1.py: test1.py:
import test2
test2.py: 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. 如果单击www.mywebsite.com/test2.py,则在屏幕上会收到“ hello world”。 However, if I click www.mywebsite.com/test1.py, I get "500 internal server error."
但是,如果我单击www.mywebsite.com/test1.py,则会收到“ 500内部服务器错误”。 I found out that it is some problem with not being able to import my modules.
我发现无法导入模块存在一些问题。
ps since I am on a shared server, I can not changes sys path etc.... ps,因为我在共享服务器上,所以我无法更改sys path等。
Here is a trace I got in errors.log: 这是我在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" 在最后一行中,您可以看到服务器写入“ ImportError:No module named test2”
How can I fix it? 我该如何解决?
Modules should be in the Python path or in the same directory where the executable is. 模块应位于Python路径中或可执行文件所在的目录中。
Then you do like usually 那你平时喜欢
import test2
test2.some_function() # or whatever.
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
假设模块的路径是:
/srv/modules/xxxx.py
首先,您应该在程序中import xxxx
模块: 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. 命令:
export PYTHONPATH=/srv/modules
。然后运行程序。
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. 使用上面的任何一种方法,您都应该成功运行程序。希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.