Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
trying to figure out useful info for running python on dreamhost shared hosting - intended for twitter bot makers

python for botmakers, on dreamhost shared hosting

On a shared hosting service like dreamhost, how do you get your twitter bot up and running? Problems:

  • where should I put my script?
  • you can't install python modules like tweepy (for twitter access) because you don't have root permission
  • once you get that solved, how do you run your script? cron?

I'm still figuring this stuff out myself, so nothing is clear as it should be. Hope this page will be a resource that will improve over time.

where should I put my script?

You almost definitely don't want it in your web directory if you're making a twitterbot. You can put it in your home directory or in a subdirectory of the home dir. When you make the virtual environment for it (see below) it will automatically make a subdirectory for you.

installing modules

Python's virtualenv lets you make your own private python where you can install modules without root access.

You need shell access to get everything set up, but once it's set up, you can edit your actual script over ftp or with Coda or whatever you like.

See below for an actual shell session where I made a virtualenv and installed tweepy. Here's commentary on that session.

The command virtualenv mynewbot will make a new python virtual environment in a new directory called mynewbot. Then cd mynewbot to enter that directory, and source bin/activate to tell python you want to use this virtualenv.

The first thing I tried to do in my new virtualenv was install tweepy, the python twitter module - but it failed because dreamhost supplies an ancient version of pip, the python module installer. Luckily, you can upgrade pip inside the virtualenv with the command pip install --upgrade pip

Now it's possible to install tweepy with pip install tweepy. I made a really simple python script that just tries to import tweepy, and then showed that it works fine in the virtualenv but fails outside of the virtualenv.

running your script

To test your script, you'll probably want to run it from the dreamhost shell, like in my transcript below, but later you'll want to make it run autonomously, probably from a scheduled cron job. Issues:

  • you need to tell cron to use the virtualenv
  • cron always runs from your home directory; if your script cares about the working directory (because it loads external vocabulary files or some such), you need to take that into account.

The shell script in the transcript below takes care of both those issues by turning on the virtualenv and moving into your python script's directory. Instead of running the python script directly, run the shell script and let it run the python for you.

crontab on dreamhost

Here's a screenshot from the dreamhost control panel, under goodies / crontab, for running the example dreamhost crontab screenshot

[dreamhost]$ # make a python virtual environment so you can install your own modules
[dreamhost]$ virtualenv mynewbot
New python executable in mynewbot/bin/python
Installing distribute.............................................................................................................................................................................................done.
Installing pip...............done.
[dreamhost]$ # go into the mynewbot directory
[dreamhost]$ cd mynewbot/
[dreamhost]$ # enter the virtualenv
[dreamhost]$ source bin/activate
(mynewbot)[dreamhost]$ # upgrade pip because dreamhost pip is obsolete, uggghhhhhhh
(mynewbot)[dreamhost]$ pip install --upgrade pip
Downloading/unpacking pip from
Successfully installed pip
(mynewbot)[dreamhost]$ # now you can install modules like tweepy
(mynewbot)[dreamhost]$ pip install tweepy
Successfully installed oauthlib-0.7.2 requests-2.4.3 requests-oauthlib-0.4.1 six-1.7.3 tweepy-3.1.0
(mynewbot)[dreamhost]$ # here is a little test python script
(mynewbot)[dreamhost]$ cat >
import tweepy
print "YAY! I didnt crash"
(mynewbot)[dreamhost]$ python
YAY! I didnt crash
(mynewbot)[dreamhost]$ # if i leave the virtual environment, it won't work
(mynewbot)[dreamhost]$ deactivate
[dreamhost]$ python
Traceback (most recent call last):
File "", line 1, in <module>
import tweepy
ImportError: No module named tweepy
[dreamhost]$ # I can re-enter the virtual environment
[dreamhost]$ source bin/activate
(mynewbot)[dreamhost]$ # now it works again
(mynewbot)[dreamhost]$ python
YAY! I didnt crash
(mynewbot)[dreamhost]$ deactivate
[dreamhost]$ # or i can run it like this without activating the virtualenv first
[dreamhost]$ bin/python
YAY! I didnt crash
[dreamhost]$ # that should work from cron too, if I put the whole path in cron
[dreamhost]$ /home/moonmilk/mynewbot/bin/python /home/moonmilk/mynewbot/
Yay! I didnt crash
[dreamhost]$ # Another way to run it without activating it first:
[dreamhost]$ # Make a shell wrapper
[dreamhost]$ # Advantage of this way is it sets the working directory
[dreamhost]$ # -- so if your script uses other files in the same directory
[dreamhost]$ # -- you don't have to give the full path name of those files
[dreamhost]$ # this is from
[dreamhost]$ cat >
cd /home/moonmilk/mynewbot
source bin/activate
[dreamhost]$ # did it work?
-bash: command not found
[dreamhost]$ # ugh it has to be executable
[dreamhost]$ chmod +x
[dreamhost]$ ./
YAY! I didn't crash
[dreamhost]$ # it should run from crontab ok
[dreamhost]$ # with an entry like this
[dreamhost]$ /home/moonmilk/mynewbot/
YAY! I didn't crash
[dreamhost]$ # if i want to install more python modules for my script
[dreamhost]$ # i have to remember to activate the virtualenv first
[dreamhost]$ # here I forgot to activate, so it failed to install:
[dreamhost]$ pip install pillow
Downloading/unpacking pillow
error: could not create '/usr/local/lib/python2.7/dist-packages/PIL': Permission denied
[dreamhost]$ # OK this time I will activate the virtualenv first!
[dreamhost]$ source bin/activate
(mynewbot)[dreamhost]$ pip install pillow
Installing collected packages: pillow
Successfully installed pillow-2.7.0
(mynewbot)[dreamhost]$ # YAY!

lmj0011 commented Jan 4, 2016

Thanks for this. Really help me to understand virtualenv.

Had to set my crontab to this for it to work though:
$ sh /home/moonmilk/mynewbot/
(prefix line 76 with sh command)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment