Installation For Ubuntu: Both Python 2.7 and Python 3.4 should have already installed by default. If not, you can install via: # ...

Python Installation and Getting Started

11:48:00 AM Unknown 0 Comments




 Installation

  • For Ubuntu: Both Python 2.7 and Python 3.4 should have already installed by default. If not, you can install via:
    # Installing Python 2
    $ sudo apt-get install python
    # Installing Python 3
    $ sudo apt-get install python3
    
    To verify the Python installation (under Ubuntu 14.04 LTS):
    # List packages beginning with python
    $ dpkg --list python*
    ||/ Name                Version        Architecture  
    +++-===================-==============-==============
    ii  python              2.7.5-5ubuntu3 amd64
    ii  python2.7           2.7.6-8ubuntu0 amd64
    ii  python3             3.4.0-0ubuntu2 amd64
    rc  python3.3           3.3.2-7ubuntu3 amd64
    ii  python3.4           3.4.3-1ubuntu1 amd64
    # Locate the Python Interpreters from the PATH
    $ which python*
    /usr/bin/python
    /usr/bin/python2.7
    /usr/bin/python3
    /usr/bin/python3.4
    $ ll /usr/bin/python*
    lrwxrwxrwx 1 root root       9 Dec 21  2013 python -> python2.7*
    lrwxrwxrwx 1 root root       9 Dec 21  2013 python2 -> python2.7*
    -rwxr-xr-x 1 root root 3345416 Jun 23 02:51 python2.7*
    lrwxrwxrwx 1 root root       9 Mar 23  2014 python3 -> python3.4*
    -rwxr-xr-x 2 root root 3709944 Oct 15 05:42 python3.4*
    -rwxr-xr-x 2 root root 3709944 Oct 15 05:42 python3.4m*
    lrwxrwxrwx 1 root root      10 Mar 23  2014 python3m -> python3.4m*
          # Clearly,
          # "python" and "python2" are symlinks to python2.7.
          # "python3" is a simlink to python3.4.
    # Show status of a specific package
    $ dpkg --status python2.7
    Version: 2.7.6-8ubuntu0.2
    ......
    $ dpkg --status python3.4
    Version: 3.4.3-1ubuntu1~14.04.3
    ......
  • For Windows: From http://www.python.org/download/, download the 32-bit or 64-bit MSI installer, and run the downloaded installer.
To check the version for a Python Interpreter, use -V (or --version) flag, e.g.,
$ python --version
Python 2.7.6
$ python2 -V
Python 2.7.6
$ python3 --version
Python 3.4.3
 

Getting Started

Interactive Python Command-Line Shell
You can run the Python Interpreter in interactive mode, i.e., as a command-line shell. To launch the Python Command-Line shell:
  • In Ubuntu:
    $ python
    Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
    [GCC 4.8.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
  • In Windows: Click the START button ⇒ Python ⇒ Python (Command-line); or run "python.exe" from the Python installed directory.
  • In Mac: [TODO]
The command-prompt is denoted as >>>.
You can enter Python statements, e.g.,
>>> print('hello, world') hello, world >>> x = 123 >>> x 123 >>> To exit the command-line session, type exit(), or press Ctrl-D (Ubuntu) (End-of-File (EOF)), Ctrl-Z + Enter (Windows).
First Python Script - hello.py
Use a programming text editor to write the following Python script and save as "hello.py" in a directory of your choice:
1
2
3
4
5
print('Hello, world')         # Print a string
print(2 ** 88)                # Print 2 raises to the power of 88
                              # Python's integer is unlimited in size!
print(8.01234567890123456789) # Print a float
print((1+2j) * (3*4j))        # Python supports complex numbers!
Program Notes:
  • Statements beginning with a # until the end-of-line are comments.
  • The print() function can be used to print a value to the console.
  • Python's strings can be enclosed with single quotes '...' or double quotes "..." (Line 1).
  • Python's integer is unlimited in size (Line 2).
  • Python support floats (Line 4).
  • Python supports complex numbers (Line 5) and other high-level data types.
  • By convention, Python script (module) filenames are in all-lowercase.
The expected outputs are:
Hello, world 309485009821345068724781056 8.012345678901234 (-24+12j)
Running Python Scripts
You can develop/run a Python script in many ways - explained in the following sections.
Running Python Scripts via System Command Shell
You can run a python script via the Python Interpreter under the System's Command Shell (e.g., Windows Command Shell, Linux/UNIX/Mac Terminal/Bash Shell). The Python Interpreter shall be included in the PATH.
  • In Linux/Mac Terminal/Bash Shell:
    $ cd <dirname>     # Change directory to where you stored the script
    $ python hello.py  # Run the script via the Python interpreter
    See below for writing executable Python script in Unix.
  • In Windows Command Prompt: Start a CMD by entering "cmd" in the start menu.
    > cd <dirname>     # Change directory to where you stored the script
     
    > python hello.py  # Run the script via the Python Interpreter
    > hello.py         # if ".py" file is associated with Python Interpreter
Unix Executable Shell Script
In Linux/UNIX, you can turn a Python script into an executable program (called Shell Script or Executable Script) by:
  1. Start with a line beginning with #! (called "hash-bang" or "she-bang"), followed by the full-path name to the Python Interpreter, e.g.,
    #!/usr/bin/python
    print('Hello, world')
    print(2 ** 88)
    print(8.01234567890123456789)
    print((1+2j) * (3*4j))
    To locate the Python Interpreter, use command "which python" or "which python3".
  2. Make the file executable via chmod (change file mode) command:
    $ cd /path/to/project-directory
    $ chmod u+x hello.py  # enable executable for user-owner
    $ ls -l hello.py      # list to check the executable flag
    -rwxrw-r-- 1 uuuu gggg 314 Nov  4 13:21 hello.py
  3. You can then run the Python script just like any executable program. The system will look for the Python Interpreter from the she-bang line.
    $ cd /path/to/project-directory
    $ ./hello.py
The drawback is that you have to hard code the path to the Python Interpreter, which may prevent the program from being portable across different machines.
Alternatively, you can use
#!/usr/bin/env python ...... The env utility will locate the Python Interpreter (from the PATH entries).
Running Python Scripts inside Python Command-Line Shell
To run a script inside Python's command-line shell:
$ python ...... >>> exec(open('/path/to/hello.py').read())
  • You can use either absolute or relative path for the filename. But, '~' (for home directory) does not work?!
  • The open() function open the file, in default read-only mode.
  • The read() function reads the entire file.
Environment Variables PATH and PYTHONPATH, and sys.path
The environment variable PATH shall include the path to Python Interpreter "python".
Python system variable sys.path is a list of directories for searching Python modules. It is initialized from the environment variable PYTHONPATH, plus an installation-dependent default. The PYTHONPATH, by default, is empty.
To show the sys.path for the Python Interpreter:
$ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) ...... >>> import sys >>> sys.path ['', '/usr/lib/python2.7', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', ......] $ python3 Python 3.4.3 (default, Oct 14 2015, 20:28:29) ...... >>> import sys >>> sys.path ['', '/usr/lib/python3.4', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages', ......]
 

0 comments: