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.
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.
Python's virtualenv lets you make your own private python where you can install modules without root access. http://docs.python-guide.org/en/latest/dev/virtualenvs/
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 dreamhostpython.sh-session 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.
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 test.sh
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.
Here's a screenshot from the dreamhost control panel, under goodies / crontab, for running the example test.sh
:
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/test.sh
(prefix line 76 with sh command)