Introduction
This is a tutorial for starting to use Python in a scientific environment. If you wonder why you should care, please refer to the enthusiastic page. I work on Windows, so these tutorials will refer to Win systems. Anyway, given the fact Python is available for OsX and Linux, it won’t be hard for the reader to figure out the alternative route in those few platform-specific steps.
Goal
At the end of this tutorial, you will have a Python system ready for scientific use. You will learn:
- Differences between Python versions
- How to install new modules/libraries
- What are the common development environments
- What are the most common Python libraries used for science
note: If you want to follow the easy way, you can install Enthought Python Distribution.
Installing Python
Python is an interpreter, a program that reads a text file and and then acts depending on each line of code. A Python script is a text file (with a .py extension) that is read by the Python machine (an executable file, i.e. python.exe). All the rest is the Python environment, a suite of modules, programs, editors, batch files and links to help the programmer.
First one needs to install the Python suite.
Go on www.python.org and download version 2.7 x32 for your system. (note: picking the x32bit is a valid and safe option even for x64 systems).
Unix systems should have Python already installed. Verify that the version is 2.7
What’s up with Python 3? Python had been slowly evolving, but yet to maintains backward compatibility. Only in recent time (8ish years) it was decided to make more substantial changes to the language, and a new main release was born. Since Python has a huge userbase, both 2 and 3 versions are supported until most of the users have done the transition. At the time of writing, the main scientific libraries have just started offering a Python 3 compatible version, but many have not updated yet, so it’s better to play it safe, sticking with 2.7
Now try to execute the python command line. Either look in the installed programs, or open the Command Prompt and type python. You should see something similar to
[code]
Python 2.7 (#1, Feb 28 2010, 00:02:06)
Type "help", "copyright", "credits" or "license" for more information.
>>>
[/code]
This is it. I won’t go into Python syntax at this point. Refer to the next tutorial, or to the official introduction.
Trick: Place ‘python.exe’ in the system path
Trick: Verify that double-clicking on a .py file executes it as Python script. If you are on Windows, you’ll probably just see a flash of a DOS window. This is because once the script is over, the operating system closes it immediately: a quick fix is to end the script with a breaking command (i.e. ‘raw_input()’ or ‘while True: pass’ ), later on we will introduce some developing environments that take care of this problem.
Installing New Modules
Even though “Python comes with batteries included”, that is, the standard library is very extensive and rich, the default installation is not sufficient for most of the scientific applications. Rather then reinventing the wheel, one can rely on external modules or libraries (a set of modules) that extend the language.
Every Python module is a python file, containing the new functionality. We’ll see in the next tutorial how to actually load modules, and the only requirement in order to use it is to indicate to Python where it is located: that means you can just copy the module file into the working directory (i.e. the directory where the interpreter is executed) or include it to the python path, even during execution, and start using it immediately.
Python has also a ‘library’ directory, and in it a ‘site-package’ that is the designated directory for third-parties libraries. All modules in that directory are automatically included to the path, and accessible to every instance of Python
Indeed one would like more sophisticated possibility (i.e. a library could require configuration depending on the OS, or is based on some c++ code that needs to be compiled): for this there is ‘distutils’, and it comes in 2 flavors.
The simplest thing you can come across is the standalone installer: it is built against a specific Python version and OS. You just double click on it, and follow the wizard. You can try it (win) with the Python Image Library.
If the standalone is not provide, the library comes with a setup.py, that contains the installing script. In order to install, you need to call (from command prompt)
[code]
python setup.py install
[/code]
and it will install the library in the proper third-party library directory. For more installation options, refer to the readme.
This is it for installing new modules, but there is an additional tool that makes managing the library easier. setuptools is a module that installs a couple of scripts in the python directory (i.e. ‘python27/scripts’). If you call
[code]
python easy_install.py name_of_the_module
[/code]
the script will check in several databases to find module with the given name, and will install it. If it fails, you can refer to it’s errors for completing the installation (i.e. missing dependency). It doesn’t work 100% of the time, but it’s a very useful tool. It is even more useful in updating the installed modules. You can do just typing
[code]
python easy_install.py –upgrade name_of_the_module
[/code]
Science Libraries
Now that you have all the tools to upgrade your vanilla Python distribution, let’s look for scientist essential libraries.
NumPy: is “the fundamental package for scientific computing with Python. It contains among other things:
- a powerful N-dimensional array object
- sophisticated (broadcasting) functions
- tools for integrating C/C++ and Fortran code
- useful linear algebra, Fourier transform, and random number capabilities”
Scipy: it contains advanced numerical mathematical routines, among all DFFTs, optimization, regression and ODE integration. Most of this tools are compiled Fortran or C programs, so they perform fast. Scipy is also the name of the community for the scientific use of Python.
Matplotlib: enables graphical output, plots and graphs. It can be used simple mathematical graphical plotter, with a syntax similar to matlab (pylab). In addition, it provides an extremely powerful api for more complex applications.
Try to install all these 3 libraries with any of the methods from the previous section. We will learn a little bit of how to use them in the next tutorial, but you should always refer to their documentation for knowing how to use them.
Choosing the Environment
The last thing to prepare is the developing environment, that is which editor and pipeline you want to use for coding. I will list here few options that were very convenient in my experience in the lab, but they are not the only or the best. In the next tutorial we will employ each of this tools, but now let’s just list them:
Idle
Notepad++ (or any other augmented text-editor)
Ipython
Spyder
Eclipse
I blog frequently and I seriously appreciate your information. Your article has
truly peaked my interest. I’m going to take a note of your site and keep checking for
new details about once a week. I subscribed to your RSS feed too.
What’s up, its nice article on the topic of media print, we all be aware of media is a great source of data.
Very good post. I’m experiencing some of these issues as well..
Its like you read my mind! You appear to know
a lot about this, like you wrote the book in it or something.
I think that you could do with some pics to drive the
message home a little bit, but instead of that, this is fantastic blog.
An excellent read. I will certainly be back.
Thanks for every other fantastic post. Where else could anybody get that type of information in such
an ideal manner of writing? I have a presentation next week, and
I’m on the look for such information.
I have been browsing on-line more than 3 hours as of late, but I by no means discovered
any attention-grabbing article like yours. It’s
pretty worth enough for me. In my view, if all site owners and bloggers made excellent content
as you did, the web can be a lot more useful than ever before.
Aw, this was an exceptionally nice post. Finding the time and actual effort
to create a really good article… but what can I say…
I put things off a lot and don’t manage to get nearly anything done.
If you wish for to take a great deal from this
piece of writing then you have to apply these techniques to your won blog.
This article is actually a pleasant one it helps new net users,
who are wishing in favor of blogging.
Generally I don’t read post on blogs, but I would like to
say that this write-up very compelled me to check out
and do so! Your writing taste has been amazed me. Thank you, quite great post.
Hey! I could have sworn I’ve been to this blog before but after
browsing through some of the post I realized it’s new to me.
Nonetheless, I’m definitely glad I found it and I’ll be book-marking and checking back frequently!
Does your blog have a contact page? I’m having trouble locating it but, I’d like
to send you an email. I’ve got some recommendations for your
blog you might be interested in hearing. Either way, great site and
I look forward to seeing it grow over time.
Wow, amazing blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your site is fantastic, as well as the content!
Quality posts is the important to be a focus for the people to pay
a visit the web site, that’s what this web page is providing.
Excellent goods from you, man. I have understand your stuff previous to and you’re just extremely wonderful.
I actually like what you’ve acquired here, certainly like
what you are stating and the way in which you say it.
You make it enjoyable and you still take care of to keep it wise.
I can’t wait to read far more from you. This is actually a terrific
site.
Appreciating the time and energy you put
into your site and in depth information you present.
It’s nice to come across a blog every once in a while that isn’t the same outdated rehashed material.
Fantastic read! I’ve bookmarked your site and I’m including your RSS feeds
to my Google account.
I for all time emailed this web site post
page to all my associates, as if like to read
it afterward my contacts will too.
Excellent goods from you, man. I have understand your
stuff previous to and you’re just too wonderful. I really like what
you’ve acquired here, really like what you are stating and the way in which you say it.
You make it entertaining and you still care for to
keep it sensible. I cant wait to read much more from you.
This is actually a wonderful site.
Your means of telling everything in this piece of writing is
in fact good, every one be capable of effortlessly understand it, Thanks a lot.
My partner and I stumbled over here from a different page and thought
I might check things out. I like what I see so i am just following you.
Look forward to finding out about your web page again.
Hello! I just would like to give you a huge thumbs up for your
excellent info you’ve got right here on this post.
I am returning to your website for more soon.
Does your website have a contact page? I’m having a
tough time locating it but, I’d like to send you an email.
I’ve got some recommendations for your blog you might be interested in hearing.
Either way, great website and I look forward to seeing it develop over time.
Hello it’s me, I am also visiting this site on a regular basis, this web page is really
nice and the users are really sharing pleasant thoughts.
Hi there, of course this paragraph is actually fastidious and I have learned lot
of things from it on the topic of blogging. thanks.
Good day! I could have sworn I’ve been to this site before but after
browsing through some of the post I realized it’s new to me.
Anyways, I’m definitely glad I found it and
I’ll be bookmarking and checking back often!
Attractive section of content. I just stumbled upon your weblog and in accession capital to assert that
I acquire in fact enjoyed account your blog
posts. Any way I’ll be subscribing to your augment and even I
achievement you access consistently quickly.
Wow that was strange. I just wrote an extremely long comment but after I clicked submit my comment
didn’t show up. Grrrr… well I’m not writing all that over again.
Anyhow, just wanted to say great blog!
Highly descriptive article, I enjoyed that a
lot. Will there be a part 2?
I’m amazed, I must say. Seldom do I come across a blog that’s equally educative and interesting, and let me tell
you, you’ve hit the nail on the head. The problem is something too few people
are speaking intelligently about. I am very happy I came across this
in my hunt for something concerning this.
Good day! I know this is kinda off topic but I was wondering which blog platform are you using for
this site? I’m getting fed up of WordPress because I’ve had issues with hackers and I’m looking at alternatives for another platform.
I would be great if you could point me in the direction of
a good platform.
Very good article. I certainly love this site. Keep it up!
I have been exploring for a little bit for any high quality articles
or weblog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this site.
Studying this information So i am glad to show that I have a very
excellent uncanny feeling I came upon exactly what I
needed. I such a lot for sure will make sure to do not disregard this web site and provides it a look regularly.
Very good information. Lucky me I came across your website by accident (stumbleupon).
I’ve book marked it for later!
We’re a group of volunteers and starting a new scheme in our community.
Your web site provided us with valuable information to work on. You’ve done an impressive job and our entire community will be thankful to you.
Hello Dear, are you truly visiting this web site daily, if so then you will absolutely get nice know-how.
Hi, Neat post. There’s an issue with your web site in web explorer, could test
this? IE nonetheless is the marketplace leader
and a large component of folks will pass
over your fantastic writing because of this problem.
Hi there! I could have sworn I’ve been to this website before
but after browsing through some of the post I realized it’s new to me.
Nonetheless, I’m definitely happy I found it and I’ll be bookmarking and checking back often!
I have been exploring for a bit for any high-quality articles or weblog posts in this kind
of space . Exploring in Yahoo I eventually stumbled upon this site.
Reading this info So i’m glad to exhibit that I have a very excellent uncanny
feeling I found out just what I needed. I such a lot undoubtedly will make sure to do not put out of your mind this website and
give it a glance on a continuing basis.
When I initially left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox and now each time a comment is added I receive four emails with the exact same comment. Perhaps there is a way you are able to remove me from that service? Thanks!
Very nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed surfing around your weblog posts.
After all I’ll be subscribing to your feed and I
am hoping you write again very soon!
Hey just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Chrome.
I’m not sure if this is a format issue or something to
do with web browser compatibility but I figured I’d post to
let you know. The style and design look great though!
Hope you get the problem solved soon. Cheers
Hi my family member! I wish to say that this article is amazing, nice written and include approximately all significant infos.
I would like to see extra posts like this .
Asking questions are in fact nice thing if you are not understanding anything totally, however this article provides good understanding even.
I am really glad to read this blog posts which consists of plenty of useful
data, thanks for providing these kinds of data.
Thanks very nice blog!
Hey! Someone in my Myspace group shared this website with us so I came to look it over.
I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers!
Superb blog and brilliant style and design.
Way cool! Some extremely valid points! I appreciate you writing
this post plus the rest of the site is extremely
good.
hey there and thank you for your information – I have certainly picked up
anything new from right here. I did however expertise several technical points using this website,
as I experienced to reload the site lots of times previous to I could get it to load correctly.
I had been wondering if your hosting is OK? Not that I am complaining,
but slow loading instances times will often affect your placement in google and can damage your
high quality score if ads and marketing with Adwords. Anyway
I am adding this RSS to my e-mail and could look out for
a lot more of your respective interesting content.
Make sure you update this again soon.
Hi, all is going nicely here and ofcourse every
one is sharing facts, that’s truly fine, keep up writing.
Oh my goodness! Impressive article dude! Thank
you, However I am having issues with your RSS.
I don’t know why I can’t join it. Is there anyone else getting identical RSS problems?
Anybody who knows the answer will you kindly respond?
Thanx!!