From charlesreid1

 
(28 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==


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.
[[Python/Golf]]


Install a version of Python from the above Python.org web site.
==Awesome Python==


Using the Python.org version of Python will install the Python executable to the following location:
Awesome-python: https://awesome-python.com/ (github repo here: [https://github.com/vinta/awesome-python])


<pre>
Wow, just... wow.
/Library/Frameworks/Python.framework
</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
==Learning Python==


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


There is also a "Current" version that will point to the most up-to-date version of Python.
Why you shouldn't use "Learn Python the Hard Way": http://sopython.com/wiki/LPTHW_Complaints


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).
List of recommended Python tutorials: http://sopython.com/wiki/What_tutorial_should_I_read%3F


(Alternatively, the Python installer may automatically add this to your path by modifying .bash_profile itself).
Ebook: Dive Into Python 3: http://www.diveintopython3.net/


== Linux ==
=Installing and Uninstalling Modules=


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/
==Installing Automatically==


For instructions related to building software from source, go here: [[Compiling Software]]
To install a package automatically for Python 2:


<pre>
pip install mymodule
# or
pip2 install mymodule
</pre>


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


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


You can download the latest version of Python for Windows from this page: http://www.python.org/download/
For Python3,


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. 
<pre>
pip3 install mymodule
</pre>


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).
==Installing Manually==


==Virtual Python: virtualenv==
===System Wide Installation===


If you don't want to deal with the difficulties of installing a system version of Python, or you don't have the permissions to do so, you have another option: install a virtual version of Python, called virtualenv.
To install system-wide:


This can be obtained from here: http://pypi.python.org/pypi/virtualenv
<pre>
cd mymodule
python setup.py build
python setup.py install
</pre>


Essentially you install it to a prefix, where it will create a /prefix/bin/ and /prefix/lib/ directory.  Then a python binary is put into /prefix/bin/, and it wraps the system python and so can load all relevant libraries, etc., but can also load libraries in /prefix/lib/ so that you can extend Python, without ever touching the system version of Python.
===User-Specific===


Install it by running:
To install a module for a single user, use the --user flag:
 
In Python2:


<pre>
<pre>
$ python setup.py install --prefix=${HOME}/pkg/virtualenv
cd mymodule
python setup.py build
python setup.py install --user
</pre>
</pre>


or wherever you want to install it.
In Python3, the above command results in this error:
 
<pre>
error: can't combine user with prefix, exec_prefix/home, or install_(plat)base
</pre>


Then, whenever you want to install a Python package and make it loadable by virtualenv, you just append the same <code>--prefix</code> to the end of setup.py:
So add an empty --prefix flag:


<pre>
<pre>
$ cd /path/to/package
python setup.py build
$ python setup.py install --prefix=${HOME}/pkg/virtualenv
python setup.py install --user --prefix=
</pre>
</pre>


==Uninstalling Automatically==


= Python Settings =
To uninstall something using pip, just tell it uninstall:
 
<pre>
pip uninstall mymodule
</pre>


== Python Path ==
==Uninstalling Manually==


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):
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.


<syntaxhighlight lang="python">
When installing, use the --record flag:
>> 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>
python3 setup.py install --user --prefix= --record files.txt
</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:
Then, when you're ready to uninstall, feed files.txt to the remove command:


<syntaxhighlight lang="bash">
<pre>
export $PYTHONPATH="${PYTHONPATH}:/path/to/module"
cat files.txt | xargs rm -rf
</syntaxhighlight>
</pre>


=Packages=


==General Instructions==
=Building Packages=


In most cases, you can install Python packages using a three-step process.
==Setup.py==


The first step is to build the package:
See [[Python/Setup.py]]


<source lang="bash">
$ python ./setup.py build
</source>


The second step is to install the package.  This is where you would specify a prefix if you don't want the package to be installed system-wide:
=Checking Across Versions=


<source lang="bash">
To check if something installs OK across versions of Python, use this bash script:
$ python ./setup.py install
</source>


or
<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>


<source lang="bash">
via [https://bitbucket.org/ruamel/yaml/issues/133/error-modulenotfounderror-is-not-defined]
$ python ./setup.py install --prefix=/path/to/package
</source>


The third and final step is to add <code>/path/to/package</code> to your <code>$PYTHONPATH</code> variable.  The location of the necessary Python stuff can be found by tacking on a <code>lib/python2.7/site-packages/</code> to the end of <code>/path/to/package</code>. 
=Removing=


So you would add the following path to your <code>$PYTHONPATH</code> variable:
==Removing Python.org Python==


<source lang="bash">
Via http://bugs.python.org/issue7107:
export PYTHONPATH="/path/to/package/lib/python2.7/sitepackages:${PYTHONPATH}"
</source>


This would go in one of your [[Dot files]], like <code>.profile</code> or <code>.bashrc</code>.
<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>


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


<pre>
  ${tmpfile} 2.6
</pre>


==Py4Sci==
To actually delete the files:


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


The Py4Sci (Python 4 Science) suite consists of 4 Python extensions, which combine to provide a Matlab-like environment. These extensions are:
=Tests=


* [http://ipython.scipy.org/moin/ iPython] - provides an enhanced Python shell
how to write tests in Python:
* [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


While the official installation instructions provided by the Numpy and Scipy projects are helpful, they are minimal.  I've got more installation instructions at the [[Py4Sci]] page.
[[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