1

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

akosch
  • 4,326
  • 7
  • 59
  • 80
  • What do you have control over on your host? What server do you use? Can you write arbitrary configs for the server? What operating system and architecture is your host? Those details are necessary but not sufficient conditions to getting a good answer. – David Berger Nov 18 '09 at 21:49
  • I have control over my home directory incl ~/public_html, my provider uses apache and I can't access its main configuration files, but it is configured fine for basic CGI. Host has some custom linux installed on a x86_64 box – akosch Nov 19 '09 at 00:15
  • It would help to add to the title of the question "on a server with no python available". People seeing this question who would be best served by virtualenv aren't going to see that answer, because of your unusual requirements. – Jeffrey Harris Nov 19 '09 at 16:19

4 Answers4

4

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.

bobince
  • 528,062
  • 107
  • 651
  • 834
1

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

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
0

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.

jathanism
  • 33,067
  • 9
  • 68
  • 86
  • I've found this myself and it's not what i need: I don't want to use the systems Python version... I need a separate environment with my own Python, not just different libs. – akosch Nov 19 '09 at 00:21
  • That is what is nice about virtualenv. You don't have to use the system's Python version. You can use any Python binary you want so long as you can execute it on your system. Virtualenv allows you to have totally isolated execution environments for different projects, or in this case from the system at large. Each virtualenv you create is an entirely separate instance of Python. You can have one for each project, application or daemon if you so desire. Here is a video tutorial that illustrates how easy it is to use: http://showmedo.com/videotutorials/video?name=2910000&fromSeriesID=291 – jathanism Nov 19 '09 at 01:21
  • sry if i was unclear: i can't use virtualenv or easy_install because they need python to work and I don't have a working python environment on that system. I need to set up python from scratch in my home dir – akosch Nov 19 '09 at 02:52
0

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.

Community
  • 1
  • 1
Heikki Toivonen
  • 30,964
  • 11
  • 42
  • 44