Updated post here: https://codeyarns.github.io/tech/2014-02-25-how-to-use-file-open-dialog-to-get-file-path-in-python.html
Tag: python
How to access command-line arguments in Python
In a C program, the command-line arguments passed to the program can be accessed using the argc and argv input parameters to the main function. Accessing the command-line arguments passed to a Python program are easy to access too. These arguments are available as a list named argv in the sys module. At a minimum, there is at least one argument in this list. This is the first argument, which is the name of the Python script.
This Python program prints out its command-line arguments:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| def main(): | |
| for arg in sys.argv: | |
| print arg | |
| if "__main__" == __name__: | |
| main() |
How to generate XKCD plot using Matplotlib

Don’t you think those plots, charts and graphs used in XKCD comic strips look really cool? I also think that by mimicking handwriting, these plots actually make the data more understandable. But, that’s just me.
Matplotlib now includes support for XKCD plots! And it is really easy to use. If you are already using Matplotlib to generate plots for your data, then you can generate XKCD plots for those easily:
- Check if the version of Matplotlib on your computer supports XKCD plots. You can do this with the Python interpreter:
$ python >>> import matplotlib.pyplot >>> matplotlib.pyplot.xkcd() <matplotlib.rc_context object at 0x1ff9950>
If you see the above object being created then your Matplotlib supports XKCD plots. Jump to step 4. If you get a AttributeError: 'module' object has no attribute 'xkcd' error, then you need to build the latest version of Matplotlib from source. See the next two steps.
- Before I built Matplotlib, I needed to install the following packages:
$ sudo apt-get install tcl-dev tk-dev $ sudo pip install --upgrade distribute
I did these because building Matplotlib needs TCL and TK libraries for displaying plots and it needs a recent version of distribute.
- Uninstall your existing Matplotlib and use
pipto build and install the latest version for you:
$ sudo apt-get remove python-matplotlib $ sudo pip install matplotlib
Hopefully, that installation should work. If it fails during building then check what libraries its missing and install those packages.
- XKCD plots use a font named Humor Sans. Download it from here. To install it follow the steps described here.
-
You are ready to generate XKCD plots. Just add the call
matplotlib.pyplot.xkcd()before the other plotting calls in plot generating Python code. The generated plots will now be like in XKCD! π
Tried with: Matplotlib 1.3.0 and Ubuntu 12.04 LTS
How to add gridlines in Matplotlib
Gridlines are the horizontal and vertical dotted lines that run across a plot. These aid in visual understanding of a plot. Adding gridlines is really easy in Matplotlib:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import matplotlib.pyplot as pyplot | |
| # Your other plotting calls go here | |
| pyplot.grid(True) |
Tried with: Python 2.7.3 and Ubuntu 12.04 LTS
How to read contents of zip file in Python
pip cheatsheet
Installing and managing packages of Python is a pain. Many popular Python packages are available for installation from Ubuntu repositories, but they are old versions.
The most convenient way to install, uninstall and manage Python packages is Pip.
- pip is the usually the Python2 tool. pip3 is the Python3 tool. All the commands in this post will use pip, but they apply the same to pip3.
-
pip and pip3 can be installed from Ubuntu repositories:
$ sudo apt install python-pip python3-pip
- To install a package from PyPI for all users:
$ sudo pip install foobar
This installs the package files to /usr/local/lib/python2.x/site-packages for pip and to /usr/local/lib/python3.x/site-packages for pip3.
- To install a package from PyPI for only yourself:
$ pip install --user foobar
This installs the package files to ~/.local/lib/python2.x/site-packages.
- To uninstall a package that was installed for all users:
$ sudo pip uninstall foobar
- To uninstall a package that was installed only for you:
$ pip uninstall --user foobar
- To upgrade an installed package:
$ pip install --upgrade foobar
- To list all installed packages and their versions:
$ pip list
- To search PyPI for a package by name:
$ pip search foobar
- To list the installed file paths of a package:
$ pip show -f foobar
How to add a shebang to Python files
By adding a shebang to your Python source file and making it executable, you can run it directly at the shell.
The most straightforward shebang that can be added is a path to the Python executable. For example, on my computer this is:
#!/usr/bin/python
But the preferred method seems to be to use the env program:
#!/usr/bin/env python
By using env, you do not have to worry where the Python executable is located. env will just use whatever python it can find first in the PATH.
Tried with: Ubuntu 12.04 LTS
How to read a text file compressed using bzip2 in Python
One of the reasons that Python is awesome is that there is a package for almost anything you can imagine. For example, if you are handling text files of large sizes, then it is good idea to store them in a compressed format, like bzip2 for example.
It is easy to read in the lines of such a text file compressed using bzip2 in Python using the bz2 package:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import bz2 | |
| bz_file = bz2.BZ2File("foo.txt.bz2") | |
| line_list = bz_file.readlines() |
Tried with: Python 2.7.3 and Ubuntu 12.04 LTS
A strange error on running Python script at the shell
Problem
I have a Python script named foo.py. Since, I would like to run it directly from the shell, I have added a shebang to run it through Python:
#!/usr/bin/env python print "Hello world"
If I run it directly at the shell, I get a strange error:
$ ./foo.py : No such file or directory
However, if I run it explicitly with the Python interpreter, it works fine:
$ python foo.py Hello world
Solution
This strange error turned out to be due to the newline markers in the file. The file was created on Windows and had CR+LF as the newline marker. But, I was running the script on Linux, which only uses LF as the newline marker.
So, this must have led the env program to run the character CR as a command. And obviously, it could not find any such file or directory.
The solution is simple. Convert the newline markers in the file to that of the host system, in this case to Linux. This can be done easily in Vim by using the fileformat option.
Tried with: GNU CoreUtils 8.12.197, Python 2.7.3 and Ubuntu 12.04 LTS
Maching learning in Python with scikit-learn
scikit-learn is a Python package that provides a lot of the algorithms and procedures needed to do machine learning. I primarily use it to generate ROC curves and compute AUC.
For Ubuntu users, scikit-learn can be installed using the package name python-scikits-learn.
Tried with: Ubuntu 12.04 LTS