部署 python CGI 应用程序

发布于 2024-08-12 03:15:23 字数 207 浏览 3 评论 0原文

我开发了一个 python CGI 应用程序,它在我的开发盒上运行得很好。然而,我的托管提供商几乎无法控制其服务器:我在 python 环境中使用了很多自定义内容(例如 sqlalchemy 和 mako 模板),并且服务器的 python 版本太旧而无法使用。我的问题是:如何在我的主目录中设置一个隔离的、完整的、独立的 python 环境并安装运行我的应用程序所需的模块? ...最简单的方法;)

I have developed a python CGI application which works just fine on my development box. My hosting provider however gives me little control of its server: I use a lot of custom stuff in my python environment (like sqlalchemy and mako templating) and the servers python version is far too old to be used. My question is: how do I set up a isolated, complete, standalone python environment in my home directory and install my required modules to run my app? ...the easiest way ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怀里藏娇 2024-08-19 03:15:23

如何在我的主目录中设置一个隔离的、完整的、独立的 python 环境

  1. (如果它尚不存在。您不必使用 >.local 但它正在成为放置此内容的正常位置)
  2. mkdir /home/me/.local/src (同上)
  3. cd /home/me/.local/ src
  4. wget http://python.org/ftp/python/2.6.4/Python-2.6.4.tgz
  5. gzip -d Python-2.6.4.tgz< /code>
  6. tar xf Python-2.6.4.tar
  7. cd Python-2.6.4
  8. ./configure --prefix=/home/me/.local< /code>
  9. make
  10. make install

希望你现在可以运行 Python:

  • /home/me/.local/bin/python

安装你需要的包通常的 setup.py 脚本,但使用您的 Python 版本:

  • /home/me/.local/bin/python setup.py install

在 CGI 文件上设置 hashbang 以使用您的 Python 版本:

  • #!/home/me/.local/bin/python

如果可以的话,考虑将您的应用程序迁移到 WSGI。当然,您仍然可以使用 wsgiref.handlers 通过 CGI 部署 WSGI 应用程序。目前,您可以使用 CGIHandler,但将来当您的托管环境不那么糟糕时,您将能够使用浪费更少的服务器接口(例如 mod_wsgi)进行部署。

how do I set up a isolated, complete, standalone python environment in my home directory

  1. mkdir /home/me/.local (if it doesn't already exist. You don't have to use .local but it is becoming the normal place to put this)
  2. mkdir /home/me/.local/src (ditto)
  3. cd /home/me/.local/src
  4. wget http://python.org/ftp/python/2.6.4/Python-2.6.4.tgz
  5. gzip -d Python-2.6.4.tgz
  6. tar xf Python-2.6.4.tar
  7. cd Python-2.6.4
  8. ./configure --prefix=/home/me/.local
  9. make
  10. make install

Hopefully you can now run Python:

  • /home/me/.local/bin/python

Install packages you need using the usual setup.py script, but with your version of Python:

  • /home/me/.local/bin/python setup.py install

Set hashbang on CGI files to use your version of Python:

  • #!/home/me/.local/bin/python

Consider migrating your application to WSGI if you can. You can of course still deploy WSGI apps through CGI using a wsgiref.handlers.CGIHandler for now, but in the future when you have a less woeful hosting environment you'll be able to deploy using a much less wasteful server interface such as mod_wsgi.

梦行七里 2024-08-19 03:15:23

站在你的立场上,我会使用 pyinstaller 将 Python、我的代码和所有依赖项捆绑到一个安装程序中可执行文件,上传并运行它。只要确保使用 pyinstaller 的 SVN 主干——“发布”版本已经过时了。

请注意,使用 SQLAlchemy 和其他所有工具以及 CGI,您可能会发现速度非常慢,因为每次访问页面时您都需要支付全额启动费用。但如果你能负担得起 CGI,我想这就是我会尝试应对的方式!-)

In your shoes, I'd use pyinstaller to bundle Python, my code, and all my dependencies into one installer executable, upload it, and run it. Just be sure to use the SVN trunk of pyinstaller -- the "released" version is WAY obsolete.

Be aware that with SQLAlchemy and everything else, with CGI you may find out you're really slow, since you're paying the full startup price everytime the page gets visited. But if CGI is all you can afford, I guess that's the way I would try to cope!-)

笑饮青盏花 2024-08-19 03:15:23

这看起来像是 virtualenv 的工作。从网站:

此外,如果无法将软件包安装到全局 site-packages 目录中怎么办?例如,在共享主机上。

这看起来正合你的胃口。

This looks like a job for virtualenv. From the site:

Also, what if you can't install packages into the global site-packages directory? For instance, on a shared host.

This looks to be right up your alley.

痴情 2024-08-19 03:15:23

我使用 Dreamhost 的共享套餐。除了 CGI 之外,他们还提供 FastCGI ,这使得事情比 CGI 快得多。您应该检查您的托管提供商是否提供此服务。或者他们可能提供 Passenger for Ruby,您可以使用它来搭载 Python。

如果您自己编译 Python,如果您尝试安装预编译包并遇到失败,请记住 UCS 设置。请参阅 StackOverflow 文章。 Dreamhost 的 wiki 有一些关于如何在他们的服务器上构建和部署 Python 的建议;您可能想要适应您的需求。

I am on Dreamhost's shared plan. Besides CGI, they also offer FastCGI which makes things much faster than CGI. You should check if your hosting provider offers that. Or maybe they provide Passenger for Ruby that you could piggyback your Python with.

If you compile Python yourself, keep in mind the UCS setting if you try to install precompiled packages and experience failures. See the StackOverflow article. Dreamhost's wiki has some advice on how you could build and deploy Python yourself on their servers; you might want to adapt to your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文