|
2008-02-12
Tags: Programming How to set up a nice Python development environmentA recommended way to start developing in Python in Linux Newbies to the Python programming language usually ask for an IDE. First of all, Python is a language that does not really need an IDE. You can just use a text editor like Kate, with a very simple text completion based on words that already exist in the file being edited. I have been using 2 very good simple IDEs for Python: Eric 4 and Geany. (Sometimes I try to use Eclipse/PyDev too, but I hate it when it hangs!) Instead of worrying about an IDE, make sure you install ipython. It is an enhanced Python interpreter, full of features such as tab completion, help, documentation viewing etc. However, ipython usage is beyond the scope of this post. As a new Python developer there are some things that you should be worried about, but maybe you don't know. These things gradually led me to set up a development environment with these characteristics:
If you are doing web development, note that this approach can also be used in shared hosting accounts where you have no root access! The following tutorial shows how to accomplish these goals in Linux. We use nano as a text editor, but you can use any text editor you want. You also should know about file names that start with a dot: they are usually hidden from the user in Linux, and used for configuration and for programs to store user data. In Nautilus, the Gnome file manager, you can toggle visibility of hidden files by pressing Control-H. Step 0: UninstallFirst of all, uninstall any special Python packages that you may have installed from your distribution's repositories, such as TurboGears, SQLObject, SQLAlchemy, Kid, Genshi, Django etc. From your distro, keep only your Python, or better yet, just what your distro uses. There is really no need to custom install Python itself. Hopefully you already have Python 2.5, 2.6 and 3.0... You can also apt-get install setuptools -- that is ok. Step 1: Prepare program locationsOpen a console to create the following directories: The last 2 directories is where we will install all Python packages, using setuptools. Now we want to tell Python to look up those directories when searching for some Python package. So edit your .bashrc file: At the top of that file, add these 2 lines: That adds the libraries to the Python path and the startable programs to the shell path. This will happen the next time the shell starts and reads the .bashrc file. We also want that to take effect now. So go ahead and paste those 2 lines to your console, too. To see your current settings use these commands:
Step 2: Configure setuptoolsNow we want setuptools to use those 2 directories too, so when we say easy_install TurboGears it will be installed in ~/python/lib. You can always use easy_install --prefix ~/python/lib TurboGears, but it is tedious to specify the prefix everytime. So let us make that a default place. Edit your ~/.pydistutils.cfg file: In that file insert: Now easy_install will install everything to ~/python/lib and ~/python/bin! Step 3: Install your stuffHere is an example of using Python setuptools to install the necessary eggs for developing an old version of Webpyte: Here is an explanation: -U means "find the latest available version"; -Z means "unzip the egg file". Because Python is able to run zipped programs, they have created this glorified zip file (with a standard structure) that the call an egg. Each egg contains programs and metadata -- information about itself. For instance, it knows its dependencies. The easy_install command takes care of dependencies for you. Because it is easier to see the source files if they aren't zipped, I like to use easy_install -Z, which extracts the eggs. So you see, an installed egg can be either a zip file (.egg) or a directory. But where do these eggs come from? From the Cheese Shop, now known as Python Package Index: http://pypi.python.org -- this is the place where you search for nice packages! Step 4: Uninstalling and upgradingThere is no remove command. To uninstall an egg, you just delete its directory and erase its line from the text file easy-install.pth. Usually you don't need to remove anything. setuptools supports multiple versions of the same package. The easy-install.pth file determines which version is being used. Therefore, to upgrade to the newest turbogears, it is usually enough to just type: Anyway, this is how I currently work in Ubuntu Linux. If you have any suggestions, I would like to hear them. Maybe an even better solution?Weeks after writing this article I learned that virtualenv exists and has a nice reputation. I haven't tried it yet, but it looks good. |