From charlesreid1

 
(31 intermediate revisions by the same user not shown)
Line 1: Line 1:
[http://www.python.org Python] is a handy language whose syntax is easy to learn.  Python is a scripting language that is similar in syntax to Matlab, but has the power of object-oriented languages such as C++.  Python is also extensible, and many libraries and packages are available for nearly every function imaginable.  For example, Numpy and Scipy provide tools often used in scientific programming, such as matrix and vector objects, not inherently provided by Python.
Python - the computer language


= Installing Python =
=Python Modules=


== Mac ==


Mac comes with a version of Python built-in, but this version of Python is 2.5, at least 2 major versions behind the latest.  It is recommended that you install the latest version of Python 2.x (stick with 2.x, since Python 3.x makes some very major changes and probably won't work as expected).
==Security/Networking==


Python versions can be obtained here: http://www.python.org/download/releases/
[[Scapy]]


Pick the version you want, and when you click on it, you'll be offered downloads of the source code and binaries for several platforms.
[[Olipy]]


===Location of Installed Python===
[[Pyrit]]


Mac OS X comes with a built-in version of Python, located at
==Computing/Numerics==


<pre>
[[Numpy]]
/usr/bin/python
 
</pre>
[[Cantera]]
 
[[Fipy]]
 
[[Python Sundials]]
 
==Data==
 
[[Pandas]]
 
==Images==
 
[[Python Imaging Library]]
 
=A Few Python Gems=
 
[[Python/One-Liners]]
 
=Profiling=
 
==Profiling Python Code==
 
See [[Python/Profiling]]
 
==Timing Python Code==
 
See [[Python/Timing]]
 
==Sizeof Python Lists==
 
To illustrate table doubling and checking the "real" size of a list (under the hood) based on the memory allocated and not just the number of elements:
 
See [[Arrays/Python/Sizeof]]
 
=Resources=
 
==This Wiki==
 
All pages on this wiki categorized Python: [[:Category:Python]]
 
The old Python page: [[Old Python Page]]
 
==Code Golf with Python==
 
[[Python/Golf]]
 
==Awesome Python==


This is the wrong version of python, as it's a system version. It's best to just leave this version alone, as the system still uses it to do different things.
Awesome-python: https://awesome-python.com/ (github repo here: [https://github.com/vinta/awesome-python])


Install a version of Python from the above Python.org web site.
Wow, just... wow.


Using the Python.org version of Python will install the Python executable to the following location:
==Learning Python==


<pre>
<pre>
/Library/Frameworks/Python.framework
# it's about damn time
alias python='python3'
</pre>
</pre>


There is a "Python" binary in this folder which always points to the latest version of Python. Alternatively, you can find specific versions of Python at
Why you shouldn't use "Learn Python the Hard Way": http://sopython.com/wiki/LPTHW_Complaints


<pre>
List of recommended Python tutorials: http://sopython.com/wiki/What_tutorial_should_I_read%3F
/Library/Frameworks/Python.framework/Versions/2.7/python
</pre>


There is also a "Current" version that will point to the most up-to-date version of Python.
Ebook: Dive Into Python 3: http://www.diveintopython3.net/


Once you've installed the Python.org version of Python, you will need to add these locations to your path, in order to use this version of Python.  This can be done with your [[Dot files]] (such as .profile or .bash_profile).
=Installing and Uninstalling Modules=


(Alternatively, the Python installer may automatically add this to your path by modifying .bash_profile itself).
==Installing Automatically==


== Linux ==
To install a package automatically for Python 2:


You can use your package manager to install a binary Python, or you can download the source code from here: http://www.python.org/download/
<pre>
pip install mymodule
# or
pip2 install mymodule
</pre>


For instructions related to building software from source, go here: [[Compiling Software]]
to update all packages it depends on, use the -U flag:


<pre>
pip install -U mymodule
</pre>


For Python3,


== Windows ==
<pre>
pip3 install mymodule
</pre>


You can download the latest version of Python for Windows from this page: http://www.python.org/download/
==Installing Manually==


It is highly recommended that you install Python in the root of your C drive: when it asks you for an install location, specify C:\Python27. 
===System Wide Installation===


You can then add folder locations to your <code>$PYTHONPATH</code> variable by editing the Windows environment variables.  On Windows XP, right-click on My Computer, click the "Advanced" tab, and click the "Environmental Variables" button.  On Windows 7, right-click on Computer, click the "Advanced settings..." link on the left-hand side, and click the "Environmental Variables" button.  Once you're there, you'll create a new user variable: variable name <code>PYTHONPATH</code> and variable value <code>C:\Python27</code> (if you want to add other locations, separate them using a semicolon; don't delete any locations that are already there, just add locations).
To install system-wide:


<pre>
cd mymodule
python setup.py build
python setup.py install
</pre>


===User-Specific===


= Python Settings =
To install a module for a single user, use the --user flag:


== Python Path ==
In Python2:


Python uses modules, which are basically libraries of functions or code that can be downloaded and used. Some examples include Numpy and Scipy (described in further detail below, in the [[Python#Py4Sci|Py4Sci]] section. To import a module named <code>foo</code>, you would type (at the Python shell/interpreter):
<pre>
cd mymodule
python setup.py build
python setup.py install --user
</pre>


<syntaxhighlight lang="python">
In Python3, the above command results in this error:
>> import foo
</syntaxhighlight>


When this is typed, Python looks in a couple of different locations for the module, contained in a file named <code>foo.py</code>.  These locations are defined in an environmental variable called <code>$PYTHONPATH</code>.  So basically, if you want to  write your own module, or if you install a module that does not automatically update <code>$PYTHONPATH</code> (most modules will), you will need to modify <code>$PYTHONPATH</code> manually. 
<pre>
error: can't combine user with prefix, exec_prefix/home, or install_(plat)base
</pre>


This variable is created with the same syntax as the system $<code>PATH</code> variable; that is, locations are separated by commas, and assuming the module foo.py is in <code>/path/to/module</code>, you can add to the existing value of <code>$PYTHONPATH</code> by putting this in your .profile file:
So add an empty --prefix flag:


<syntaxhighlight lang="bash">
<pre>
export $PYTHONPATH="${PYTHONPATH}:/path/to/module"
python setup.py build
</syntaxhighlight>
python setup.py install --user --prefix=
</pre>


=Packages=
==Uninstalling Automatically==


==Py4Sci==
To uninstall something using pip, just tell it uninstall:


The Py4Sci (Python 4 Science) suite consists of 4 Python extensions, which combine to provide a Matlab-like environment. These extensions are:
<pre>
pip uninstall mymodule
</pre>


* [http://ipython.scipy.org/moin/ iPython] - provides an enhanced Python shell
==Uninstalling Manually==
* [http://numpy.scipy.org/ Numpy] - an extension providing numerical routines for vector and matrix objects
* [http://www.scipy.org/ Scipy] - an extension providing MATLAB-like functionality (optimization, Fourier transforms, ODE solvers, etc.), typically used in conjunction with data types provided through the Numpy extension
* [http://matplotlib.sourceforge.net/ Matplotlib] - provides 2D plotting functionality to Python


This is a bit more tricky, and requires you do some preparation when you install the package (or at least remember how you installed it). When you run setup.py to install software, you can tell it to make a record of every file it updates. Then, to uninstall, you can just remove all of those files.


When installing, use the --record flag:


===Py4Sci on Linux===
<pre>
python3 setup.py install --user --prefix= --record files.txt
</pre>


This is very straightforward, given that most every distribution's package manager has the above 4 Python extensions (in fact, many distributions already have at least some of the above packages, esp. iPython). Use the following commands for Debian-based distributions (e.g. Ubuntu):
Then, when you're ready to uninstall, feed files.txt to the remove command:


<pre>
<pre>
$ apt-get install ipython
cat files.txt | xargs rm -rf
$ apt-get install numpy
$ apt-get install scipy
$ apt-get install matplotlib
</pre>
</pre>


where one may replace "apt-get" with the appropriate package manager (e.g. "yum" for Fedora).


=== Py4Sci on Mac ===
=Building Packages=


This was formerly a very difficult and frustrating process, which has since become much easier and much more streamlined.
==Setup.py==


Instructions for installing SciPy and NumPy on Mac OS X are here: http://www.scipy.org/Installing_SciPy/Mac_OS_X
See [[Python/Setup.py]]


Installing Matplotlib is easy too: just download the source code, or your platform's binary, from the Matplotlib SourceForge page: http://sourceforge.net/projects/matplotlib/


Finally, you can download iPython for Mac by going to the iPython downloads page: http://ipython.scipy.org/moin/Download (installation instructions are contained in the iPython documentation, linked from that page)
=Checking Across Versions=
 
To check if something installs OK across versions of Python, use this bash script:
 
<pre>
for i in 2.7 3.3 3.4 3.5 3.6; do
  mktmpenv -p /tmp/python/$i/bin/python --no-wheel
  pip install mymodule
  deactivate
done
</pre>


<!--
via [https://bitbucket.org/ruamel/yaml/issues/133/error-modulenotfounderror-is-not-defined]


Three links provide the information needed to install the above extensions for Python on Mac OS X.
=Removing=


# Read about the role of built-in Pythons and various other available Pythons on the Mac: [http://www.python.org/download/mac/ http://www.python.org/download/mac/]
==Removing Python.org Python==
# Read about the various Python distributions for Mac here: [http://wiki.python.org/moin/MacPython/PythonDistributionsForMac http://wiki.python.org/moin/MacPython/PythonDistributionsForMac]
# Download the Python binary from here: [http://www.python.org/download/releases/ http://www.python.org/download/releases/]
-->


Via http://bugs.python.org/issue7107:


<pre>
tmpfile=/tmp/generate_file_list
cat <<"NOEXPAND" > "${tmpfile}"
#!/bin/sh
version="${1:-"2.6"}"
file -h /usr/local/bin/* | grep \
"symbolic link to ../../../Library/Frameworks/Python.framework/"\
"Versions/${version}" | cut -d : -f 1
echo "/Library/Frameworks/Python.framework/Versions/${version}"
echo "/Applications/Python ${version}"
set -- Applications Documentation Framework ProfileChanges \
        SystemFixes UnixTools
for package do
  echo "/Library/Receipts/Python${package}-${version}.pkg"
done
NOEXPAND
chmod  ug+x ${tmpfile}
</pre>


===Py4Sci on Windows===
This script lists all files/top-level directories to be removed:


The instructions are similar to those for Mac...
<pre>
  ${tmpfile} 2.6
</pre>


SciPy and NumPy installation instructions here: http://www.scipy.org/Installing_SciPy/Windows
To actually delete the files:


Matplotlib installation can be done using the Windows binary, available here: http://sourceforge.net/projects/matplotlib/
<pre>
  ${tmpfile} 2.6 | sed -e "s/^.*$/sudo rm -r \"&\"/g" | sh
</pre>


iPython installation is also easy, using the Windows binary provided here: http://ipython.scipy.org/moin/Download
=Tests=


how to write tests in Python:


[[Python/Tests]]


==Cantera==


Cantera is a package that provides a Python interface for performing thermochecmial calculations.  You can find information about installing Cantera at the [[Cantera]] page.  For examples of how to use Cantera, including the Python interface, visit the [[Cantera Lecture]] page.


=Flags=


{{PythonFlag}}


[[Category:Computers]]
[[Category:Python]]
[[Category:Languages]]

Latest revision as of 15:55, 12 March 2019

Python - the computer language

Python Modules

Security/Networking

Scapy

Olipy

Pyrit

Computing/Numerics

Numpy

Cantera

Fipy

Python Sundials

Data

Pandas

Images

Python Imaging Library

A Few Python Gems

Python/One-Liners

Profiling

Profiling Python Code

See Python/Profiling

Timing Python Code

See Python/Timing

Sizeof Python Lists

To illustrate table doubling and checking the "real" size of a list (under the hood) based on the memory allocated and not just the number of elements:

See Arrays/Python/Sizeof

Resources

This Wiki

All pages on this wiki categorized Python: Category:Python

The old Python page: Old Python Page

Code Golf with Python

Python/Golf

Awesome Python

Awesome-python: https://awesome-python.com/ (github repo here: [1])

Wow, just... wow.

Learning Python

# it's about damn time
alias python='python3'

Why you shouldn't use "Learn Python the Hard Way": http://sopython.com/wiki/LPTHW_Complaints

List of recommended Python tutorials: http://sopython.com/wiki/What_tutorial_should_I_read%3F

Ebook: Dive Into Python 3: http://www.diveintopython3.net/

Installing and Uninstalling Modules

Installing Automatically

To install a package automatically for Python 2:

pip install mymodule
# or
pip2 install mymodule

to update all packages it depends on, use the -U flag:

pip install -U mymodule

For Python3,

pip3 install mymodule

Installing Manually

System Wide Installation

To install system-wide:

cd mymodule
python setup.py build
python setup.py install

User-Specific

To install a module for a single user, use the --user flag:

In Python2:

cd mymodule
python setup.py build
python setup.py install --user

In Python3, the above command results in this error:

error: can't combine user with prefix, exec_prefix/home, or install_(plat)base

So add an empty --prefix flag:

python setup.py build
python setup.py install --user --prefix=

Uninstalling Automatically

To uninstall something using pip, just tell it uninstall:

pip uninstall mymodule

Uninstalling Manually

This is a bit more tricky, and requires you do some preparation when you install the package (or at least remember how you installed it). When you run setup.py to install software, you can tell it to make a record of every file it updates. Then, to uninstall, you can just remove all of those files.

When installing, use the --record flag:

python3 setup.py install --user --prefix= --record files.txt

Then, when you're ready to uninstall, feed files.txt to the remove command:

cat files.txt | xargs rm -rf


Building Packages

Setup.py

See Python/Setup.py


Checking Across Versions

To check if something installs OK across versions of Python, use this bash script:

for i in 2.7 3.3 3.4 3.5 3.6; do 
  mktmpenv -p /tmp/python/$i/bin/python --no-wheel 
  pip install mymodule
  deactivate
done

via [2]

Removing

Removing Python.org Python

Via http://bugs.python.org/issue7107:

tmpfile=/tmp/generate_file_list
cat <<"NOEXPAND" > "${tmpfile}"
#!/bin/sh
version="${1:-"2.6"}"
file -h /usr/local/bin/* | grep \
"symbolic link to ../../../Library/Frameworks/Python.framework/"\
"Versions/${version}" | cut -d : -f 1
echo "/Library/Frameworks/Python.framework/Versions/${version}"
echo "/Applications/Python ${version}"
set -- Applications Documentation Framework ProfileChanges \
         SystemFixes UnixTools
for package do
  echo "/Library/Receipts/Python${package}-${version}.pkg"
done
NOEXPAND
chmod  ug+x ${tmpfile}

This script lists all files/top-level directories to be removed:

  ${tmpfile} 2.6

To actually delete the files:

  ${tmpfile} 2.6 | sed -e "s/^.*$/sudo rm -r \"&\"/g" | sh

Tests

how to write tests in Python:

Python/Tests


Flags