From charlesreid1

(Created page with "[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...")
 
 
(43 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


= Packages =
=Python Modules=


== Py4Sci ==


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


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


[[Olipy]]


[[Pyrit]]


=== Py4Sci on Linux ===
==Computing/Numerics==


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):
[[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: [https://github.com/vinta/awesome-python])
 
Wow, just... wow.
 
==Learning Python==


<pre>
<pre>
$ apt-get install ipython
# it's about damn time
$ apt-get install numpy
alias python='python3'
$ 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).
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>
error: can't combine user with prefix, exec_prefix/home, or install_(plat)base
</pre>
 
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:


[[Python/Tests]]




=== Py4Sci on Mac ===


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


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


# 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/]
[[Category: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/]

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