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 was very pleased to uncover this great site. I need to to thank you for ones time for this fantastic read!! I definitely appreciated every bit of it and I have you bookmarked to look at new information on your blog.
I was very pleased to uncover this great site. I need to to thank you for ones time for this fantastic read!! I definitely appreciated every bit of it and I have you bookmarked to look at new information on your blog.
Waiting for news https://www.asiansearch.info/arts-entertainment/pinupca
Piece of writing writing is also a excitement, if you be acquainted with after that you can write otherwise it
is complex to write.
A motivating discussion is worth comment. I believe that
you need to write more about this topic, it might not be a taboo matter
but typically people do not talk about these topics.
To the next! Many thanks!!
I know this if off topic but I’m looking into starting my own blog
and was wondering what all is required to get setup?
I’m assuming having a blog like yours would cost a pretty penny?
I’m not very web savvy so I’m not 100% sure. Any suggestions or advice would be greatly
appreciated. Many thanks
What’s up, I log on to your blog like every week.
Your writing style is awesome, keep doing what you’re
doing!
Way cool! Some very valid points! I appreciate you penning this article and also the rest of the site
is really good.
Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you! By the way, how could we communicate?
I blog often and I seriously thank you for your content.
This great article has really peaked my interest. I will take a note of
your site and keep checking for new details about once per week.
I subscribed to your Feed too.
This text is invaluable. When can I find out more?
Appreciate this post. Let me try it out.
Greetings! Very helpful advice in this particular article!
It’s the little changes that produce the most
significant changes. Many thanks for sharing!
Everyone loves what you guys are up too. This kind of clever work and coverage!
Keep up the terrific works guys I’ve incorporated you guys to blogroll.
Fantastic beat ! I would like to apprentice while you amend your web site, how could i subscribe for a blog site?
The account aided me a acceptable deal. I had been a little bit acquainted
of this your broadcast offered bright clear idea
Way cool! Some very valid points! I appreciate you writing this write-up plus the rest
of the website is extremely good.
I’m extremely inspired with your writing skills as well as with the format to your weblog.
Is that this a paid theme or did you customize it yourself?
Either way keep up the excellent high quality writing, it’s uncommon to peer a nice weblog like
this one nowadays..
It’s very simple to find out any matter on net as compared to textbooks, as I found this
post at this site.
Greetings from Carolina! I’m bored at work so I decided to browse your blog on my iphone during lunch break.
I enjoy the knowledge you provide here and can’t
wait to take a look when I get home. I’m shocked
at how fast your blog loaded on my phone .. I’m not
even using WIFI, just 3G .. Anyways, excellent blog!
This is my first time go to see at here and i
am in fact impressed to read everthing at one place.
This is a really good tip especially to those fresh to the blogosphere.
Simple but very accurate info… Many thanks
for sharing this one. A must read post!
This paragraph will assist the internet viewers
for creating new blog or even a blog from start to end.
Fantastic post however , I was wondering if
you could write a litte more on this subject? I’d be very thankful if
you could elaborate a little bit more. Appreciate it!
Good way of describing, and fastidious article to obtain facts on the topic of my presentation focus, which i am going to present in institution of higher education.
I just like the helpful info you provide on your articles.
I will bookmark your blog and take a look at
once more right here regularly. I’m reasonably certain I will be told plenty of new stuff proper right here!
Best of luck for the following!
Spot on with this write-up, I seriously think this amazing site needs
far more attention. I’ll probably be back again to see more, thanks for the information!
When some one searches for his vital thing, therefore he/she wishes to
be available that in detail, so that thing
is maintained over here.
Hello Dear, are you really visiting this website on a regular basis,
if so afterward you will without doubt take nice know-how.
I’ve been browsing online more than three hours lately, but I by no means found
any interesting article like yours. It’s beautiful worth sufficient for me.
In my opinion, if all website owners and bloggers made good content as you probably did, the web can be a lot
more useful than ever before.
My partner and I absolutely love your blog and find
a lot of your post’s to be exactly I’m looking for. can you offer guest writers to write content for you?
I wouldn’t mind writing a post or elaborating on a lot of
the subjects you write related to here. Again, awesome blog!
Wow, that’s what I was exploring for, what a data!
existing here at this website, thanks admin of this web
site.
I am genuinely delighted to read this website posts
which carries tons of helpful facts, thanks for providing these kinds of information.
Valuable information. Lucky me I discovered your site by accident, and I am surprised why this
twist of fate did not came about in advance!
I bookmarked it.
Hello, i feel that i saw you visited my blog thus i got here to return the prefer?.I’m attempting
to find things to enhance my website!I suppose its good enough to make use of a few of your ideas!!
Thankfulness to my father who shared with me about this website,
this weblog is truly amazing.
When I initially commented I clicked the “Notify me when new comments are added” checkbox
and now each time a comment is added I get several emails with the same comment.
Is there any way you can remove people from that service?
Appreciate it!
My relatives every time say that I am wasting my time here
at net, but I know I am getting knowledge every day by reading thes fastidious articles or reviews.
Excellent write-up. I certainly love this site. Keep it up!
What i do not realize is in reality how you’re now not actually much
more well-favored than you may be now. You are
so intelligent. You realize thus considerably on the
subject of this matter, made me individually believe it from numerous numerous angles.
Its like men and women are not interested unless it’s one thing to do with Girl gaga!
Your own stuffs nice. At all times deal with
it up!
Saved as a favorite, I love your blog!
Thank you, I’ve recently been searching for information approximately this subject for a while and yours is the
greatest I have found out so far. However, what in regards to the conclusion? Are you sure about the supply?
hello!,I love your writing very so much! percentage we keep in touch extra approximately your article on AOL?
I require an expert in this area to resolve my problem.
May be that’s you! Having a look forward to see you.
Quality articles or reviews is the main to interest the viewers to visit the web page, that’s what this site is providing.
Hi i am kavin, its my first occasion to commenting anywhere, when i read this paragraph i thought i could also make comment due to this good post.
Exceptional post but I was wondering if you could write a litte more on this topic?
I’d be very grateful if you could elaborate a little bit more.
Kudos!
Thanks for sharing such a pleasant thought, piece of writing is nice,
thats why i have read it completely
I am not positive the place you are getting your info, however
great topic. I needs to spend a while learning much
more or understanding more. Thank you for excellent info I was searching for this information for my
mission.
This info is invaluable. How can I find out more?
If you want to take a good deal from this piece of writing then you have to apply these
techniques to your won blog.
What a material of un-ambiguity and preserveness of valuable
knowledge concerning unexpected feelings.