environment mac os x Programming Python: matplotlib package management pip Python virtualenv
by bseanvt
2 comments
Installing MatPlotLib on OS X for Python Version 2.6.1 with PIP and VirtualEnv
If you thought you had installed matplotlib only to find this
File "/Library/Python/2.6/site-packages/matplotlib-0.91.1-py2.6-macosx-10.6-universal.egg/matplotlib/numerix/__init__.py", line 166, in
__import__('ma', g, l)
File "/Library/Python/2.6/site-packages/matplotlib-0.91.1-py2.6-macosx-10.6-universal.egg/matplotlib/numerix/ma/__init__.py", line 16, in
from numpy.core.ma import *
ImportError: No module named ma
It is because the package being installed is version 0.91 and you need at least version 1.0 .
If it’s already installed pass pip the upgrade flag and specify the package location with the “-f” flag
pip install --upgrade -f http://downloads.sourceforge.net/project/matplotlib/matplotlib/matplotlib-1.0/matplotlib-1.0.0.tar.gz matplotlib
If not installed
pip install -f http://downloads.sourceforge.net/project/matplotlib/matplotlib/matplotlib-1.0/matplotlib-1.0.0.tar.gz matplotlib
Resources:
http://stackoverflow.com/questions/3555551/why-does-pip-install-matplotlib-version-0-91-1-when-pypi-shows-version-1-0-0
Python: .htacess apache framework modpython_gateway mod_python mod_rewrite Python web.py
by bseanvt
3 comments
Mod_Python and Web.py on Ubuntu
Download
First install mod_python for Apache and then restart/reload the server.
apt-get install libapache2-mod-python /etc/init.d/apache2 force-reload apache2ctl restart
Next grab the web.py framework from webpy.org. You can grab the tar or use easy_install depending on your setup.
wget http://webpy.org/static/web.py-0.33.tar.gz tar xzvf web.py.tar.gz cd web sudo python setup.py install # or if you use easy_install easy_install web.py
Download this file http://www.aminus.net/browser/modpython_gateway.py?rev=106&format=raw This python package can (with this version) go anywhere in your python path “sys.path”. However, I placed it in the wsgiref directory. I’m running python2.6 so I did the following
# after downloading mv modpython_gateway.py /usr/lib/python2.6/wsgiref/modpython_gateway.py
It’s important to remember that in the VirtualHost file you create the PythonHandler directive will need to reference this module. If you place it outside of the wsgiref directory remember to change the directive as well.
The VirtualHost
Now we need to create a VirtualHost for our application. You can run web.py apps without apache and this is good for development mode. To do this the command is
python pcode.py 4567
where the pcode.py is your web.py application filename and the 4567 is the port number you want to start the server on. I think it defaults to 8080. If you’re running apache or another web server port 80 will be in use. This is where mod_python (or cgi or fastcgi) come into play.
<VirtualHost *>
ServerName py.seanbehan.com
DocumentRoot /var/www/python/myapp
#Aliases can trip you up! Pay attention here
Alias /myapp /var/www/python/myapp
<Directory /var/www/python/myapp>
<IfModule python_module>
PythonPath "sys.path +['/var/www/python/myapp']"
AddHandler python-program .py
# modpython_gateway is a file you'll need to download and place in wsgiref
PythonHandler wsgiref.modpython_gateway::handler
# This is your python program
PythonOption wsgi.application pcode::main
PythonOption SCRIPT_NAME /myapp
PythonDebug on
</IfModule>
</Directory>
</VirtualHost>
Web.py Application
Web.py applications can be very simple. Everything can be placed in one file. In this example I created a file “pcode.py” and saved it to “/var/www/python/myapp/pcode.py”. The contents look like this
import web #this is the web.py frameword
web.webapi.internalerror = web.debugerror #lets capture errors for debugging
# map paths to classes
urls = (
"/", "hello"
)
# this is the main from the PythonOption wsgi.application pcode::main from our VirtualHost
main = web.application(urls, globals()).wsgifunc()
class hello:
def GET(self):
# web.header... otherwise you'll be prompted to save the file rather than view it in the browser
web.header('Content-type','text/html')
yield "Finally got web.py to work!"
if __name__ == "__main__":
main.run()
Save the file, reload apache and visit the location on the web (In my case py.seanbehan.com/pcode.py/). Remember to include that last “/” after the pcode.py. You might get a “not found” message from web.py otherwise.
Pretty URLs
You can get pretty urls like “py.seanbehan.com/hello/world” if you use the mod_rewrite apache module.
a2enmod rewrite
You can either set up the rewrite rules in your VirtualHost or in a .htaccess file. I’ll do the .htaccess so I don’t have to reload the server for the changes to take effect.
In the same directory as your pcode.py file create a file “.htaccess” and in it place this code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/icons
RewriteCond %{REQUEST_URI} !^/favicon.ico$
RewriteCond %{REQUEST_URI} !^(/.*)+code.py/
RewriteRule ^(.*)$ pcode.py/$1 [PT]
</IfModule>
Troubleshooting
I ran into some trouble with the Alias in the VirtualHost file. It looks like this
Alias /myapp /var/www/python/myapp
This will map incoming urls to the full path on the filesystem so that your python program will run properly. I kept getting the not found error message from web.py without it. There might be a better way to set this up but this is how I got it to work.
Here are some additional resources I found helpful
http://wiki.slicehost.com/doku.php?id=install_mod_wsgi_on_ubuntu_gutsy
http://webpy.org/install
http://dready.org/blog/2009/01/29/webpy-with-mod_python-on-apache/
http://www.devisland.net/help/webpy.shtml
SHA1 or MD5 Hashing in Python
import hashlib
print hashlib.sha1("My wonderful string").hexdigest()
print hashlib.md5("My other wonderful string").hexdigest()
Dynamic Attributes in Python Model Class
class Bar():
def __init__(self, **args):
for key in args:
self.__dict__[key] = args[key]
b = Bar(fullname="Monty Python", email="me@monthpython.org")
b.fullname #=> Monty Python
b.email #=>me@montypython.org
Python Zlib Compress DeCompress
import zlib regular_string = 'this is my string' compressed_string = zlib.compress(regular_string) decompressed_string = zlib.decompress(compressed_string) print compressed_string print decompressed_string
Install MySQLdb for Python on Mac OS X
I don’t do much python development. I really like the language and there are a lot of great software projects out there for it. Tornado, for example, is a fast non-blocking web server in python, just open sourced by Facebook that is the engine behind FriendFeed.com.
I downloaded the source, available at, http://tornadoweb.org, and started playing around. It comes with a database wrapper for mysql. Using “easy_install”, I suppose the python equivalent to gems, it goes something like this…
sudo easy_install MySQLdb-python
Nope :( at least not on OS X. Time to do it the hard way. Luckily not too hard. It just can’t find the right path to mysql_config. Sound familiar? http://seanbehan.com/databases/install-do_mysql-ruby-gem-on-mac-os-x/
Anyway, here are the commands that worked for me…
wget http://dl.sourceforge.net/sourceforge/mysql-python/MySQL-python-1.2.3c1.tar.gz tar xzvf MySQL-python-1.2.3c1.tar.gz cd MySQL-python-1.2.3c1
In the setup_posix.py file change
mysql_config.path = "mysql_config"
to
mysql_config.path = "/opt/local/lib/mysql5/bin/mysql_config"
Next install it with these commands
python setup.py clean python setup.py build sudo python setup.py install
That should do it.


