在内部服务器上设置Django(os.environ()没有按预期工作?)

时间:2022-05-30 22:42:52

I'm trying to setup Django on an internal company server. (No external connection to the Internet.)

我正在尝试在内部公司服务器上设置Django。 (没有外部连接到Internet。)

Looking over the server setup documentation it appears that the "Running Django on a shared-hosting provider with Apache" method seems to be the most-likely to work in this situation.

查看服务器设置文档,似乎“在使用Apache的共享托管提供程序上运行Django”方法似乎最有可能在这种情况下工作。

Here's the server information:

这是服务器信息:

  • Can't install mod_python
  • 无法安装mod_python

  • no root access
  • 没有root访问权限

  • Server is SunOs 5.6
  • 服务器是SunOs 5.6

  • Python 2.5
  • Apache/2.0.46
  • I've installed Django (and flup) using the --prefix option (reading again I probably should've used --home, but at the moment it doesn't seem to matter)
  • 我已经使用--prefix选项安装了Django(和flup)(再次阅读我可能应该使用--home,但此刻似乎并不重要)

I've added the .htaccess file and mysite.fcgi file to my root web directory as mentioned here. When I run the mysite.fcgi script from the server I get my expected output (the correct site HTML output). But, it won't when trying to access it from a browser.

我已将.htaccess文件和mysite.fcgi文件添加到我的根网站目录中,如此处所述。当我从服务器运行mysite.fcgi脚本时,我得到了我期望的输出(正确的站点HTML输出)。但是,当尝试从浏览器访问它时,它不会。

It seems that it may be a problem with the PYTHONPATH setting since I'm using the prefix option.

似乎PYTHONPATH设置可能存在问题,因为我正在使用前缀选项。

I've noticed that if I run mysite.fcgi from the command-line without setting the PYTHONPATH enviornment variable it throws the following error:

我注意到,如果我从命令行运行mysite.fcgi而不设置PYTHONPATH环境变量,则会抛出以下错误:

prompt$ python2.5 mysite.fcgi 
 ERROR:
 No module named flup   Unable to load
 the flup package.  In order to run
 django   as a FastCGI application, you
 will need to get flup from  
 http://www.saddi.com/software/flup/  
 If you've already   installed flup,
 then make sure you have it in your
 PYTHONPATH.

I've added sys.path.append(prefixpath) and os.environ['PYTHONPATH'] = prefixpath to mysite.fcgi, but if I set the enviornment variable to be empty on the command-line then run mysite.fcgi, I still get the above error.

我已经在mysite.fcgi中添加了sys.path.append(prefixpath)和os.environ ['PYTHONPATH'] = prefixpath,但是如果我在命令行中将enviornment变量设置为空,那么运行mysite.fcgi,I仍然得到上述错误。

Here are some command-line results:

以下是一些命令行结果:

>>> os.environ['PYTHONPATH'] = 'Null'
>>>
>>> os.system('echo $PYTHONPATH')
Null
>>> os.environ['PYTHONPATH'] = '/prefix/path'
>>>
>>> os.system('echo $PYTHONPATH')
/prefix/path
>>> exit()
prompt$ echo $PYTHONPATH
Null

It looks like Python is setting the variable OK, but the variable is only applicable inside of the script. Flup appears to be distributed as an .egg file, and my guess is that the egg implementation doesn't take into account variables added by os.environ['key'] = value (?) at least when installing via the --prefix option.

看起来Python正在设置变量OK,但该变量仅适用于脚本内部。 Flup似乎是作为.egg文件分发的,我的猜测是egg实现没有考虑os.environ ['key'] = value(?)添加的变量,至少在通过--prefix安装时选项。

I'm not that familiar with .pth files, but it seems that the easy-install.pth file is the one that points to flup:

我对.pth文件并不熟悉,但似乎easy-install.pth文件是指向flup的文件:

import sys; sys.__plen = len(sys.path)
./setuptools-0.6c6-py2.5.egg
./flup-1.0.1-py2.5.egg
import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sy
s.path[p:p]=new; sys.__egginsert = p+len(new)

It looks like it's doing something funky, anyway to edit this or add something to my code so it will find flup?

看起来它正在做一些时髦的事情,无论如何编辑它或在我的代码中添加一些内容以便它会发现痘痘?

4 个解决方案

#1


In your settings you have to point go actual egg file, not directory where egg file is located. It should look something like:

在您的设置中,您必须指向实际的egg文件,而不是egg文件所在的目录。它应该看起来像:

sys.path.append('/path/to/flup/egg/flup-1.0.1-py2.5.egg')

#2


Try using a utility called virtualenv. According to the official package page, "virtualenv is a tool to create isolated Python environments."

尝试使用名为virtualenv的实用程序。根据官方软件包页面,“virtualenv是一个创建孤立的Python环境的工具。”

It'll take care of the PYTHONPATH stuff for you and make it easy to correctly install Django and flup.

它将为您处理PYTHONPATH的东西,并使正确安装Django和flup变得容易。

#3


Use site.addsitedir() not os.environ['PYTHONPATH'] or sys.path.append().

使用site.addsitedir()而不是os.environ ['PYTHONPATH']或sys.path.append()。

site.addsitedir interprets the .pth files. Modifying os.environ or sys.path does not. Not in a FastCGI environment anyway.

site.addsitedir解释.pth文件。修改os.environ或sys.path不会。反正不在FastCGI环境中。

#!/user/bin/python2.6

import site

# adds a directory to sys.path and processes its .pth files
site.addsitedir('/path/to/local/prefix/site-packages/')

# avoids permissions error writing to system egg-cache
os.environ['PYTHON_EGG_CACHE'] = '/path/to/local/prefix/egg-cache'

#4


To modify the PYTHONPATH from a python script you should use:

要从python脚本修改PYTHONPATH,您应该使用:

sys.path.append("prefixpath")

Try this instead of modifying with os.environ().

试试这个而不是用os.environ()进行修改。

And I would recommend to run Django with mod_python instead of using FastCGI...

我建议用mod_python运行Django而不是使用FastCGI ......

#1


In your settings you have to point go actual egg file, not directory where egg file is located. It should look something like:

在您的设置中,您必须指向实际的egg文件,而不是egg文件所在的目录。它应该看起来像:

sys.path.append('/path/to/flup/egg/flup-1.0.1-py2.5.egg')

#2


Try using a utility called virtualenv. According to the official package page, "virtualenv is a tool to create isolated Python environments."

尝试使用名为virtualenv的实用程序。根据官方软件包页面,“virtualenv是一个创建孤立的Python环境的工具。”

It'll take care of the PYTHONPATH stuff for you and make it easy to correctly install Django and flup.

它将为您处理PYTHONPATH的东西,并使正确安装Django和flup变得容易。

#3


Use site.addsitedir() not os.environ['PYTHONPATH'] or sys.path.append().

使用site.addsitedir()而不是os.environ ['PYTHONPATH']或sys.path.append()。

site.addsitedir interprets the .pth files. Modifying os.environ or sys.path does not. Not in a FastCGI environment anyway.

site.addsitedir解释.pth文件。修改os.environ或sys.path不会。反正不在FastCGI环境中。

#!/user/bin/python2.6

import site

# adds a directory to sys.path and processes its .pth files
site.addsitedir('/path/to/local/prefix/site-packages/')

# avoids permissions error writing to system egg-cache
os.environ['PYTHON_EGG_CACHE'] = '/path/to/local/prefix/egg-cache'

#4


To modify the PYTHONPATH from a python script you should use:

要从python脚本修改PYTHONPATH,您应该使用:

sys.path.append("prefixpath")

Try this instead of modifying with os.environ().

试试这个而不是用os.environ()进行修改。

And I would recommend to run Django with mod_python instead of using FastCGI...

我建议用mod_python运行Django而不是使用FastCGI ......