Very simple Python: Difference between revisions

From AstroEdWiki
Jump to navigation Jump to search
No edit summary
Line 7: Line 7:
Python is open source software availble for free from [http://www.python.org/ www.python.org]. Version 2.7 is the current mature version that is widely supportly by other add-on modules, and is the one for which examples here are written. Python 3 is still under development and it currently lacks packages that are necessary for astronomy.   
Python is open source software availble for free from [http://www.python.org/ www.python.org]. Version 2.7 is the current mature version that is widely supportly by other add-on modules, and is the one for which examples here are written. Python 3 is still under development and it currently lacks packages that are necessary for astronomy.   


If you have Linux, then mostly likely you already have Python and you skip down to try use it.  You may need to use your package manager to augment the default installation with modules for Scipy and Numpy, but that's simple to do and will work without difficulty.
== Installing Python on your computer ==


For other operating systems, consider using the academic version from Enthought Solutions if you meet their licensing terms for the free version. This version supplies Matplotlib, Numpy and Scipy which we will use. If you do use Enthought's distribution, consider requesting a free academic license using your University email address. This will provide you with a fully-featured installation.
Python is open source software availble for free from [http://www.python.org/ www.python.org]. Version 2.7 is the current mature version that is widely supportly by other add-on modules, and is the one for which examples here are still written. Python 3 is now widely used and packages for specific disciplines that are maintained are usually compatible with it. New installations should favor Python 3, but there's not much loss of functionality with the older Python 2.7.


<center>
'''Linux'''
[http://www.enthought.com/products/edudownload.php Enthought Scientific Computing Solutions]
</center>


<center>
Python will already be installed on your computer. Use your package manager to update and add to the base installation.  You will need NumPy, SciPy, and the parts they depend on.  Also add "pip" if it is not installed, since that is the tool to allow you to add packages that may not be in your operating system's distribution.  Also check that pip and python commands on your system are for the version of python you want to useSome systems have both.
[https://www.enthought.com/academic-subscriptions/ Enthought Academic License]
</center>


'''Windows'''


Alternatively, the source of Python development has versions for MacOS and Windows of the base system that avoids the commercial (though free) route of other vendors:
For Windows there are several choices.


<center>
* [https://www.python.org/downloads/windows/ Python.org] provides installers for Windows. The web-based installer will update software components  from the web.  You may need administrator privileges to update system libraries.
[http://www.python.org/download/ www.python.org/download/]
* [https://www.enthought.com/academic-subscriptions/ Enthought Canopy] The academic subscription is free and requires a university email address. Enthought provides all the packages in one installation process, and support.  Non-academic users pay a license fee. 
</center>
* [https://winpython.github.io/ WinPython] is a portable distribution, self-contained, and can be saved and used on different computers.  It also includes several useful packages and is said to be easy to install.  It is free.


Using Python's own distribution has the advantage of simplicity and access to all of Python without a third party in the way.  It may require some knowledge of your computer's operating system, and confidence you can manage the inevitable issues that will arise. 
'''Mac OSX''


The MacOS has both its own proprietary features, and an underlying core operating system based on BSD Unix that is very similar to Linux for the usersThis structure allows you to install your own package manager, and with that to have more control on how software like Python is handledOne such system is [https://www.macports.org/install.php macports].  With it you would use a terminal on your machine and then in that terminal run
* Python 2.7 comes installed with OSX.  Try "python --version" from a terminal command line and see what happensYou can update this installation from Python.org (see next), or add package with pip given adminsitrative authorityBe aware of the potential Tkl library problem though.
* [https://www.python.org/downloads/mac-osx/ Python.org] has installers for recent Mac OS variants. However, there are problems with the Tkl libraries provided in by Apple, particularly when used for graphics and in the development environment IDLE, which you should be aware of.  Read the notice [https://www.python.org/download/mac/tcltk/  here].
*  [https://www.enthought.com/academic-subscriptions/ Enthought Canopy] The academic subscription is free for Mac users too, and requires a university email address. Enthought provides all the packages in one installation process, and support.  Non-academic users pay a license fee. 


  sudo port install python27 py27-matplotlib py27-numpy py27-scipy py27-ipython py27-pyfits
to install Python 2.7 and
  sudo port select --set python python27
  sudo port select --set ipython ipython 27
to make it available as with the command "python".  IPython is a development environment that is useful for graphics.


Those with an astronomical interest may benefit from  [http://python4astronomers.github.com/installation/python_install.html Python4Astronomers]
Those with an astronomical interest may benefit from  [http://python4astronomers.github.com/installation/python_install.html Python4Astronomers]


Most users would probably prefer running Python through the  [http://docs.python.org/2/library/idle.html IDLE] IDE, the development interface that comes with many installations(It's named after Eric Idle, who does the [http://www.youtube.com/watch?v=uo6OCxwUPPg "Galaxy Song"] in Monty Python.On the command line you would simple run "idle" to get started.  The software includes an editor and also a runtime environment.
Most users would probably prefer running Python through the  [http://docs.python.org/2/library/idle.html IDLE] integrated development environment.  This provides an editor and file management, along with help and syntax highlighting.  It's named after Eric Idle, who does the [http://www.youtube.com/watch?v=uo6OCxwUPPg "Galaxy Song"] in Monty Python. On the command line you would simple run "idle" to get started.   




Additional modules would have to be installed separately later.
Additional modules would have to be installed separately later if they are not part of the original installation. Python has its own system for adding features which makes that easy.  The ones you will need for scientific programming are


* NumPy Test with "import numpy from within interactive Python or idle.
* SciPy Test with "import scipy".
* AstroPy This one for astronomers.  Test with "import astropy".


and there are others, especially from [https://www.scipy.org/scikits.html Scikits]


== Using Python in real time ==
== Using Python in real time ==

Revision as of 04:29, 12 January 2017

In this section of our short course on Python for Physics and Astronomy we take a short path to using Python easily.


Installing Python on your computer

Python is open source software availble for free from www.python.org. Version 2.7 is the current mature version that is widely supportly by other add-on modules, and is the one for which examples here are written. Python 3 is still under development and it currently lacks packages that are necessary for astronomy.

Installing Python on your computer

Python is open source software availble for free from www.python.org. Version 2.7 is the current mature version that is widely supportly by other add-on modules, and is the one for which examples here are still written. Python 3 is now widely used and packages for specific disciplines that are maintained are usually compatible with it. New installations should favor Python 3, but there's not much loss of functionality with the older Python 2.7.

Linux

Python will already be installed on your computer. Use your package manager to update and add to the base installation. You will need NumPy, SciPy, and the parts they depend on. Also add "pip" if it is not installed, since that is the tool to allow you to add packages that may not be in your operating system's distribution. Also check that pip and python commands on your system are for the version of python you want to use. Some systems have both.

Windows

For Windows there are several choices.

  • Python.org provides installers for Windows. The web-based installer will update software components from the web. You may need administrator privileges to update system libraries.
  • Enthought Canopy The academic subscription is free and requires a university email address. Enthought provides all the packages in one installation process, and support. Non-academic users pay a license fee.
  • WinPython is a portable distribution, self-contained, and can be saved and used on different computers. It also includes several useful packages and is said to be easy to install. It is free.

'Mac OSX

  • Python 2.7 comes installed with OSX. Try "python --version" from a terminal command line and see what happens. You can update this installation from Python.org (see next), or add package with pip given adminsitrative authority. Be aware of the potential Tkl library problem though.
  • Python.org has installers for recent Mac OS variants. However, there are problems with the Tkl libraries provided in by Apple, particularly when used for graphics and in the development environment IDLE, which you should be aware of. Read the notice here.
  • Enthought Canopy The academic subscription is free for Mac users too, and requires a university email address. Enthought provides all the packages in one installation process, and support. Non-academic users pay a license fee.


Those with an astronomical interest may benefit from Python4Astronomers

Most users would probably prefer running Python through the IDLE integrated development environment. This provides an editor and file management, along with help and syntax highlighting. It's named after Eric Idle, who does the "Galaxy Song" in Monty Python. On the command line you would simple run "idle" to get started.


Additional modules would have to be installed separately later if they are not part of the original installation. Python has its own system for adding features which makes that easy. The ones you will need for scientific programming are

  • NumPy Test with "import numpy from within interactive Python or idle.
  • SciPy Test with "import scipy".
  • AstroPy This one for astronomers. Test with "import astropy".

and there are others, especially from Scikits

Using Python in real time

The first step is to figure out how to start up Python on your computer after it is installed. In Linux you open a console and type "python" on the command line. You'll immediately see a prompt that looks like ">>" after which you can type Python code and see the results.

If you installed the Enthought distribution of Python on Windows or Mac, take a look at their release notes and website for additional advice on getting started.

If you installed from the python.org, then they have some additional pages to offer help. On Windows, its not necessarily as straightforward as Linux, but it can be. It will help to read this "frequently asked question" (FAQ) page about Python on Windows to help you at first, and also consult setup and usage guide. On a Macintosh OS X system using Python is very similar to other Unix platforms like Linux or BSD. There are some helpful notes at the Using Python on a Macintosh website.

Once you have a command line prompt you have access to all of Python's capabilities. We'll show you some simple examples here to test your installation and give you a quick sense of how to use it.

To exit Python in the interactive mode, use "Ctrl+d" or "exit()" from the command line, or the Exit menu entry if you are running IDLE.


Using Python code as a standalone program

You will usually edit a file that contains your Python program and then run that program by calling the Python interpreter. Therefore, the first thing is to pick an appropriate editor. One way is to use IDLE, which makes it especially easy on Windows systems and others to edit and test with a consistent interface. On Linux systems where the command line is more commonly used, an alternative is a standard graphical editor that is aware of Python syntax like gedit. Other alternatives are nedit and emacs. Python text files have a required format, and it generally not a good idea to embed tabs in the text so the tab function has to be set for spaces instead. On MacOS your could try emacs, BBEdit, or another one of your preferred editors. The Python website maintains a list of editors and their features for different operating systems with links to the editor websites if you need to download one.

For example, if your program is in the file "myprogram.py" you can run it from the command line with "python myprogram.py". On Windows systems, the file extension ".py" may be associated with this command, and in that case you can start a program by clicking on the icon or name in a window. On MacOS and Linux, you would first make the file executable with a command such as

 chmod a+x myfile.py

and also see that the first line of the file is exactly

#!/usr/bin/python

assuming that python is installed in /usr/bin/. With those changes, any file of Python code becomes an executable program. Simply type

myfile.py

Note that programs that interact with the window manager may need to be started with pythonw instead of python. For MacOS, see 4.1.1 How to run a Python script.


Examples of very simple Python

For examples of Python illustrating how to use it interactively and to write very simple programs, see the section Python examples.


An assignment to try out very simple Python

For the assigned homework to use very simple Python interactively and as a script, see the section Python assignments.