Very simple Python: Difference between revisions

From AstroEdWiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 5: Line 5:
== Installing Python on your computer ==
== Installing Python on your computer ==


Python is open source software available for free from [http://www.python.org/ www.python.org]. Version 2.7 is the current mature version that is widely supported by other add-on modules.  Python 3 is more recent, largely compatible with 2.7, and  is now widely used and packages for specific disciplines.  New installations should favor Python 3, but there's not much loss of functionality with the older Python 2.7. We will use Python 3 for the examples, though some of the earlier files here may still require small changes to run under 3.   
Python is open source software available for free from [http://www.python.org/ www.python.org]. Version 2.7 is the aging mature version that is widely supported by other add-on modules.  Python 3 is more recent, largely compatible with 2.7, and  is now widely used with packages for specific disciplines.  New installations should be  Python 3, but there's not much loss of functionality with the older Python 2.7 if you already have it. We will use Python 3 for the examples, though some of the earlier files here may still require small changes to run under 3.   




'''Linux'''
'''Linux'''


Python will already be installed on your computer.  Typically the operating system may use 2.7 for some of its core applications. You may use your package manager to update and add to the base installations, but note the distinction between Python2 and Python3 which may co-exist.  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.  It is useful to check that pip and python commands on your system are for the version of python you want to use.  Some systems have both.  Try
Python will already be installed on your computer.  Typically the operating system may use 2.7 for some of its core applications, and provide a basic 3.4, 3.5 or 3.6 for newer work.  You may use your package manager to update and add to the base installations, but note the distinction between Python2 and Python3 which may co-exist.  Check that the pip you are using on the command line is the one to add to the version you want to use.  For example, look at


   ls -l /usr/bin/python*
   ls -l /usr/bin/python*
Line 19: Line 19:
   /etc/alternatives/pip  -> /usr/bin/pip3.4
   /etc/alternatives/pip  -> /usr/bin/pip3.4


to tell you how "pip" will run.  There may be conflicts with the operating system's requirements and what you would want for your work, but with care they can be managed and you will be in control of your own destiny.  If you prefer to let someone else drive, chose a Python distribution such as the popular [https://www.anaconda.com/distribution/ Anaconda].  
to tell you how "pip" will run.  There may be conflicts with the operating system's requirements and what you would want for your work, but with care they can be managed and you will be in control of your own destiny.  If you prefer to let someone else drive, chose a Python distribution such as the popular [https://www.anaconda.com/distribution/ Anaconda] and follow the directions on their website. Keep in mind the potential conflicts with the already-installed python on your computer.
 
 
 
'''Linux with Python from its source''
 
This method is for those who are comfortable with system management and  want to maintain full control over their Python and its packages.  It also will minimalize the installation footprint on diskspace, while adding challenges of resolving conflicts between dependencies yourself.
 
 
 
# Download the source tar file currently Python-3.6.4.tgz and as superuser or root copy to /usr/local/src
# Untar the file and assign ownership of the new directory tree to yourself as an unpriviledged user
# As a normal user, cd into the source directory and run ./configure
# The defaults will be fine.  Your new Python will go into the /usr/local/ directory.  Some users prefer /opt, which can be changed as a configuration option.
# make
# make test
# Now as root user --
# make altinstall
# ln -s /usr/local/lib64/python3.6/lib-dynload/ /usr/local/lib/python3.6/lib-dynload
 
The altinstall option is necessary to avoid overwriting or interfering with the system python.  The softlink is needed because some llibrary files in lib64 are not found without it. It is not necessary to assign either PYTHONHOME or PYTHONPATH, or to use an environment manager to have this version work independently of the system version.  However, be aware that the functions you need are explicity in /usr/local/bin and that they refer to python by its version, that is ''python3.6'' and ''pip3.6'' Therefore if you later update the OS to Leap 15 and it also has these executables, there's a potential conflict that would be resolved by the search path and could be ambiguous.
 
Similarly, if you install Anaconda Python, it will have its own /opt directory tree to navigate, while Canopy Python may use environment variables. To run your own locally built Python ''echo PYTHONHOME'' and ''echo PYTHONPATH'' should return empty strings.
 
 
'''Linux adding modules by pip'''
 
 
For installing in the system python, if you need to update the complex matplotlib package for Python 3
that may lack parts you need, it must be removed  first:
 
pip uninstall matplotlib
 
Then re-install it and specify not to use the saved source if any.
 
pip install matplotlib --upgrade --no-cache-dir
 
Also for a system python version you may need to do this
 
pip uninstall six
 
pip install six --upgrade --no-cache-dir
 
 
Now if you are building a Python for science, use the specific pip for it and add the  modules you need. This may include several that were installed on the system using yast, as well  the matplotlib ones and  these.  Start with these since pip will resolve dependencies, probably use cached source unless you tell it not to, and in the process grow the missing branches of your Python tree. Later, if you find something missing, you can add it as needed.
 
 
Install pyephem (pip install pyephem)
 
Install healpy  (pip install healpix)
 
Install reproject  (pip install reproject) for image reprojection when doing fits conversion
 
Install quantities (pip install quantities) to have physical constants
 
Install emcee  (pip install emcee) to have an MCMC library
 
Install  pyastronomy (pip install pyastronomy) or from source on github [https://github.com/sczesla/PyAstronomy pyastronomy]
 
Install scikit-image (pip install scikit-image).
 
Lastly, install the software chain for data visualization with Python using pip rather than the system package because Pandas is developing rapidly
 
Install pandas (pip install pandas)
 
Install scrapy (pip install scrapy)
 
Install requests (pip install requests)
 





Revision as of 06:13, 16 March 2018

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 available for free from www.python.org. Version 2.7 is the aging mature version that is widely supported by other add-on modules. Python 3 is more recent, largely compatible with 2.7, and is now widely used with packages for specific disciplines. New installations should be Python 3, but there's not much loss of functionality with the older Python 2.7 if you already have it. We will use Python 3 for the examples, though some of the earlier files here may still require small changes to run under 3.


Linux

Python will already be installed on your computer. Typically the operating system may use 2.7 for some of its core applications, and provide a basic 3.4, 3.5 or 3.6 for newer work. You may use your package manager to update and add to the base installations, but note the distinction between Python2 and Python3 which may co-exist. Check that the pip you are using on the command line is the one to add to the version you want to use. For example, look at

 ls -l /usr/bin/python*
 ls -l /usr/bin/pip*

to see that may be already there and what will run with the default "python" command. A trick used by Linux systems is to have a directory /etc/alternatives that contains soft links, and there you may find links such as

 /etc/alternatives/pip  -> /usr/bin/pip3.4

to tell you how "pip" will run. There may be conflicts with the operating system's requirements and what you would want for your work, but with care they can be managed and you will be in control of your own destiny. If you prefer to let someone else drive, chose a Python distribution such as the popular Anaconda and follow the directions on their website. Keep in mind the potential conflicts with the already-installed python on your computer.


'Linux with Python from its source

This method is for those who are comfortable with system management and want to maintain full control over their Python and its packages. It also will minimalize the installation footprint on diskspace, while adding challenges of resolving conflicts between dependencies yourself.


  1. Download the source tar file currently Python-3.6.4.tgz and as superuser or root copy to /usr/local/src
  2. Untar the file and assign ownership of the new directory tree to yourself as an unpriviledged user
  3. As a normal user, cd into the source directory and run ./configure
  4. The defaults will be fine. Your new Python will go into the /usr/local/ directory. Some users prefer /opt, which can be changed as a configuration option.
  5. make
  6. make test
  7. Now as root user --
  8. make altinstall
  9. ln -s /usr/local/lib64/python3.6/lib-dynload/ /usr/local/lib/python3.6/lib-dynload

The altinstall option is necessary to avoid overwriting or interfering with the system python. The softlink is needed because some llibrary files in lib64 are not found without it. It is not necessary to assign either PYTHONHOME or PYTHONPATH, or to use an environment manager to have this version work independently of the system version. However, be aware that the functions you need are explicity in /usr/local/bin and that they refer to python by its version, that is python3.6 and pip3.6 Therefore if you later update the OS to Leap 15 and it also has these executables, there's a potential conflict that would be resolved by the search path and could be ambiguous.

Similarly, if you install Anaconda Python, it will have its own /opt directory tree to navigate, while Canopy Python may use environment variables. To run your own locally built Python echo PYTHONHOME and echo PYTHONPATH should return empty strings.


Linux adding modules by pip


For installing in the system python, if you need to update the complex matplotlib package for Python 3 that may lack parts you need, it must be removed first:

pip uninstall matplotlib

Then re-install it and specify not to use the saved source if any.

pip install matplotlib --upgrade --no-cache-dir

Also for a system python version you may need to do this

pip uninstall six

pip install six --upgrade --no-cache-dir


Now if you are building a Python for science, use the specific pip for it and add the modules you need. This may include several that were installed on the system using yast, as well the matplotlib ones and these. Start with these since pip will resolve dependencies, probably use cached source unless you tell it not to, and in the process grow the missing branches of your Python tree. Later, if you find something missing, you can add it as needed.


Install pyephem (pip install pyephem)

Install healpy (pip install healpix)

Install reproject (pip install reproject) for image reprojection when doing fits conversion

Install quantities (pip install quantities) to have physical constants

Install emcee (pip install emcee) to have an MCMC library

Install pyastronomy (pip install pyastronomy) or from source on github pyastronomy

Install scikit-image (pip install scikit-image).

Lastly, install the software chain for data visualization with Python using pip rather than the system package because Pandas is developing rapidly

Install pandas (pip install pandas)

Install scrapy (pip install scrapy)

Install requests (pip install requests)


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 is a commercial distribution that is free to download, and for a fee will offer support. It is intended for scientific computing and can co-exist with the system Python of Linux.
  • Anaconda is widely used in Astronomy, and will come with all the packages you will need to get started. It uses a "conda" package management system.


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 Express is free for Mac users too. Enthought provides all the packages in one installation process, and additonal support for a fee.
  • Anaconda also has a Mac version, and is very popular.


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


IDE's and Editors and Python environments

Keep in mind that Python itself is a programming language and system. It stands on its own, and it can be incorporated into other more complex, and potentially more useful interfaces. At a minimum you will need a text editor. With that you can read and write program files, and run them as a program either by having "python" read the file, or by making the file itself executable (on Linux and Mac). Most editors on these operating systems will be fine, but some are cumbersome for learning. Unless you happen to have the skill, avoid "vi" and even "emacs", two common editors of Unix-like systems like OSX and LInux, and use something with a lighter interface. The java-based "jedit" is free, easy to install, and has some helpful features. Since it is based on java, it runs on Mac, Windows and Linux with the same look and feel. You can obtain it from http://www.jedit.org/ and follow their installation instructions.

The IDE "idle" is also very nice to start with, and recommended. It may be present after you install Python, so try the command "idle" in a terminal window and see what happens. There are said to be problems with its use of the Tkl library and the Mac OSX installed libraries, but they should be solved in the most recent releases of Python and OSX supplied by Enthought or Anaconda.

Now widely used and with great potential, the Jupyter system has been under development for a decade and is mature. You can read about it and even preview its capabilities on the web. Keep in mind if you decide to start at that level, that the system is feature-filled, and that once you create content within the system it will require the system to use that content. That is, unlike a simple Python program, the notebooks created by Jupyter are truly bodies of work that include data and analysis. It can be very useful in a lab, for example.

http://jupyter.org/


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 for Linux users are nedit and emacs, depending on your taste in interfaces. 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. 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.