Keyboard + printer
$PATH
$HOME
, ~
$PWD
stdin
, stderr
, stdout
).sh
files)For later reference.
ls
— list directory contentscd <dir>
— change directorypwd
— print (show) current working directoryman <command>
— manual for commandecho "Text $VARIABLE"
— print to stdout
apropos <topic>
— search man pagesexport PATH=~/bin:$PATH
— set PATH variableless <filename>
— read and scroll trough file<TAB>
for automatic completion<CONTROL-D>
to quit a shell (works for Python as well)<command> --help
ls --help
#!/bin/sh
# This first line tells the system it's a shell script.
# Similarly, #!/bin/usr/python would execute Python code.
# Print hello world
echo "Hello world"
# Print $PATH variable
echo "My PATH variable is $PATH"
# Change into some-directory
cd some-directory
# List directory contents
ls
# Set executable permissions
chmod +x myscript.sh
# Executing command outside of $PATH requires explicit path
./myscript.sh
~/.bash_profile
(and ~/.bashrc
)
$PATH
$PYTHONPATH
$EDITOR
Similar to bash shell: execute command, print output
$ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello world'
hello world
>>> quit()
Similar to Mathematica
$ ipython
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
Debugging with IPython-like interface; stop your code anywhere and query variables or execute arbitrary code.
One line to rule them all:
import ipdb; ipdb.set_trace()
n
— Execute next line of codes
— Step info function callc
— Continue execution as normalq
— Quit debuggerCode:
def addition(one, other):
import ipdb; ipdb.set_trace()
print 'Calculating now'
return one + other
print 'Starting calculation'
addition(2, 5)
print 'Calculation done'
Result:
$ python ipdb-example.py
Starting calculation
> ipdb-example.py(4)addition()
3
----> 4 print 'Calculating now'
5
ipdb> n
Calculating now
> ipdb-example.py(6)addition()
5
----> 6 return one + other
7
ipdb> n
--Return--
7
> ipdb-example.py(6)addition()
5
----> 6 return one + other
7
ipdb> n
> ipdb-example.py(11)<module>()
9 addition(2, 5)
10
---> 11 print 'Calculation done'
ipdb> n
Calculation done
--Return--
None
> ipdb-example.py(11)<module>()
9 addition(2, 5)
10
---> 11 print 'Calculation done'
ipdb> quit
Exiting Debugger.
setup.py
, it's an Egg.$ python setup.py install
Lot's and lot's of Eggs. Probably someone just solved your problem. In Iran. Or down the hallway.
$ pip search <package>
$ pip install <package>
$ pip install --upgrade <package>
$ pip uninstall <package>
Textfile with required packages and versions, e.g.:
pkg1
pkg2
pkg3>=1.0,<=2.0
Reference: PIP cookbook
pip install -r requirements.txt
pip freeze > requirements.txt
Warning: use only with VirtualEnv (next slide)
Sets up a 'local' Python distribution allowing you to:
Install VirtualEnv (only once):
$ pip install virtualenv
Create an isolated environment:
$ virtualenv [--system-site-packages] <dirname>
Activate the environment:
$ source <dirname>/bin/activate
Deactivate the environment:
$ deactivate
$ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xmodem
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named xmodem
$ virtualenv test
New python executable in test/bin/python
Installing Setuptools..............................................................................................................................................................................................................................done.
Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.
$ source test/bin/activate
(test) $ pip install xmodem
Downloading/unpacking xmodem
Downloading xmodem-0.3.2.tar.gz
Storing download in cache at ./.pip/download_cache/http%3A%2F%2Fpackages.crate-cdn.com%2F4%2Fe%2F5%2F7%2F4e57f3fadd1f3f043a23f1a87e9d36f944a21b552aabebcdb802154667396567%2Fxmodem-0.3.2.tar.gz
Running setup.py egg_info for package xmodem
Installing collected packages: xmodem
Running setup.py install for xmodem
Successfully installed xmodem
Cleaning up...
(test) $ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xmodem
>>>
(test) $ deactivate
$ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xmodem
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named xmodem
Find syntax, style and other errors before running your code: less bugs, more productivity and much more beautiful code.