From charlesreid1

No edit summary
 
(40 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, and get the binary from here: http://www.python.org/download/ (stick with 2.x, since Python 3.x makes some very major changes and probably won't work as expected).
==Security/Networking==


[[Scapy]]


[[Olipy]]


== Linux ==
[[Pyrit]]


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/
==Computing/Numerics==


For instructions related to building software from source, go here: [[Compiling Software]]
[[Numpy]]


[[Cantera]]


[[Fipy]]


== Windows ==
[[Python Sundials]]


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


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. 
[[Pandas]]


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).
==Images==


[[Python Imaging Library]]


=A Few Python Gems=


= Python Settings =
[[Python/One-Liners]]


== Python Path ==
=Profiling=


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):
==Profiling Python Code==


<syntaxhighlight lang="python">
See [[Python/Profiling]]
>> 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.  This variable is created with the same syntax as the system $<code>PATH</code> variable; that is, locations are separated by commas, and you can add to the existing value of <code>$PYTHONPATH</code> by putting this in your .profile file:
==Timing Python Code==


<syntaxhighlight lang="bash">
See [[Python/Timing]]
export $PYTHONPATH="${PYTHONPATH}:/path/to/foo.py"
</syntaxhighlight>


==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:


= Packages =
See [[Arrays/Python/Sizeof]]


== Py4Sci ==
=Resources=


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


* [http://ipython.scipy.org/moin/ iPython] - provides an enhanced Python shell
All pages on this wiki categorized Python: [[:Category: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


The old Python page: [[Old Python Page]]


==Code Golf with Python==


=== Py4Sci on Linux ===
[[Python/Golf]]


This is very straightforward, given that most every distribution's package manager has the above 4 Python extensions. Use the following commands for Debian-based distributions (e.g. Ubuntu):
==Awesome Python==
 
Awesome-python: https://awesome-python.com/ (github repo here: [https://github.com/vinta/awesome-python])
 
Wow, just... wow.
 
==Learning Python==
 
<pre>
# it's about damn time
alias python='python3'
</pre>
 
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:
 
<pre>
pip install mymodule
# or
pip2 install mymodule
</pre>
 
to update all packages it depends on, use the -U flag:
 
<pre>
pip install -U mymodule
</pre>
 
For Python3,
 
<pre>
pip3 install mymodule
</pre>
 
==Installing Manually==
 
===System Wide Installation===
 
To install system-wide:
 
<pre>
cd mymodule
python setup.py build
python setup.py install
</pre>
 
===User-Specific===
 
To install a module for a single user, use the --user flag:
 
In Python2:
 
<pre>
cd mymodule
python setup.py build
python setup.py install --user
</pre>
 
In Python3, the above command results in this error:


<pre>
<pre>
$ apt-get install ipython
error: can't combine user with prefix, exec_prefix/home, or install_(plat)base
$ 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).
So add an empty --prefix flag:
 
<pre>
python setup.py build
python setup.py install --user --prefix=
</pre>
 
==Uninstalling Automatically==
 
To uninstall something using pip, just tell it uninstall:
 
<pre>
pip uninstall mymodule
</pre>
 
==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:
 
<pre>
python3 setup.py install --user --prefix= --record files.txt
</pre>
 
Then, when you're ready to uninstall, feed files.txt to the remove command:
 
<pre>
cat files.txt | xargs rm -rf
</pre>
 
 
=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:
 
<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]
 
=Removing=
 
==Removing Python.org Python==
 
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>
 
This script lists all files/top-level directories to be removed:
 
<pre>
  ${tmpfile} 2.6
</pre>
 
To actually delete the files:
 
<pre>
  ${tmpfile} 2.6 | sed -e "s/^.*$/sudo rm -r \"&\"/g" | sh
</pre>


=Tests=


how to write tests in Python:


=== Py4Sci on Mac ===
[[Python/Tests]]


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


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


# 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/]
=Flags=
# 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/]


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