Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, March 15, 2008

Running the twisted deamon (twistd) on Windows

Twisted is a networking library for python, which I find really good. I've only just started playing with it, and ran into some problems when trying to get it to run as a daemon under windows (using twistd - the twisted daemonizer). The information about this on the web seemed scarce, so I decided to post my findings.

When you install twisted, it comes with a special command-shell. But after looking at it, I found that it wasn't all that special afterall. It's just a batch-script that sets up a regular shell for you, all ready to start twistd.

As I do my programming in Eclipse, I wanted to start the server directly from inside the IDE. The way I did this, was by adding a .bat file to my project. The contents of the .bat file needs to be something like this:

set PATH=%PATH%;c:\python25;c:\python25\scripts
set PATHEXT=%PATHEXT%;.py;.pyc;.pyw;.pyo
set PYTHONPATH=%PYTHONPATH%;c:\your_source_dir
cd c:\your_source_dir
twistd -noy your_server_daemon.tac
pause

You have to change three things here, the two your_source_dir and your_server_daemon.tac and also the python directory.

The first line adds the python executable to your path. In my case this is c:\python25 - you will need to change this depending on your python install. It also adds the scripts directory where your twistd.py should reside.

Next we set the PATHEXT variable, this lets you run .py files (and .pyc and so on) directly from the command line without invoking the python interpreter manually (the way it works on *nix).

After that, we add the directory (or directories) where our source files reside to the PYTHONPATH. Without this, you will get an "ImportError: No module named blabla", or "Failed to load application: No module named blabla" for all your imports. (You can also add to the pythonpath from inside your program, but I find that it's a bit cleaner this way).

Next we change into our source directory, simple as that.

After that, it's simply a matter of starting the twisted daemon (twistd) with our configuration (.tac) file. -noy means that we start the specified .tac and log to stdout (look up twistd in the twisted docs for more info). To stop the server, simply press Ctrl+C.

The pause at the end is only there so that the window doesn't close immediately after the program ends, but waits for a keypress.

More on twisted here:

Saturday, February 23, 2008

Choosing a Python development environment

If you are going to be doing any kind of serious development, you will most likely want an integrated development environment (an IDE). This will help you organize your projects and source files, and give you additional tools like automatic code completion and syntax highlighting.

Many people get by using just their favourite text-editor, be it a simple one like notepad, or more complex editors like Emacs or UltraEdit. And there is nothing wrong with that, I guess - but me I prefer to use a tool where everything is integrated.

For Python development, there are quite a few free alternatives. Idle is one of them, and comes bundled with Python when you download it. It's a no-fuzz editor and shell, and is great for those who like simplicity. It's fast and does everything you really need.

If you want to step up a bit, there is Pype, or Python Programmers' Editor which is an editor targeted specifically at Python development. It has lots of nice features which helps you speed up development.

None of these are what I would call a real IDE though. For that, look to Eclipse. This is my favourite IDE for any programming language, not only Python. It was originally intended for Java, and is written in Java, and so can be a bit more than you need and also might be a bit sluggish at times. It does however have all the features you will ever need, and more.

Eclipse is fully modular, meaning that you can add support for different languages as you go along. Java, C / C++, Python, Perl, PHP - whatever you throw at it, you can be almost certain that there is a module for Eclipse that supports your language.

Also Eclipse is free and open-source, which is one of the reasons I like it so much.

There are of course also quite a few commercial IDE's for Python. I haven't tried any of them, but some of them (like the Wing IDE) look quite good. You might want to look for some evaluation versions, if you want to fork out some cash for a professional solution (though Eclipse is VERY professional).

Check out the links:

Friday, February 22, 2008

Thinking about learning a programming language?

Have you ever had an idea for a dream application, but never knew how to program it?

A lot of people starting to program use Java or C / C++. Java is considered to be easy, because it handles things like garbage collection for you, and there is no such thing as pointers - which is something that confuses new programmers a lot in C / C++.

Most end up using C / C++ because it is the "industry standard" - but this does not automagically mean that this is the best language. If you are thinking about learning to program, I suggest you check out the Python language.

Python is a "scripting language", but is extremely powerful. It can be used with ease for programming web-pages for example in the place of PHP or Perl, but also for full blown applications with a nice user interface and all the bells and whistles.

The saying among Python programmers is that it makes you 10 times more productive than if you were programming in C++, because you have to write a lot less code. This is an important point if you are programming by yourself or in a small team, because your program is ready for market in a much shorter time - and you also have a lot less code to maintain.

By using wxWidgets for Python, you can have a multi-platform (Linux / Windows / Mac) application with a GUI ready in a few hours, if not minutes.

Python really lets you do a lot of things that C / C++ doesn't, because the philosophy is that you, the programmer, should be in charge - and decide for yourself the best way to accomplish a solution to your problem.

I recommend you check it out if you are interested in programming:

If you want a graphical GUI builder, check out wxGlade, which can generate wxWidgets source code for Python and C++