English 中文(简体)
Deploying a python CGI app
原标题:

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 ;)

最佳回答

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.

问题回答

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!-)

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.

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.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签