Module Import Errors in Python when Executing a Program

I ran into a weird issue today with a python program where even though I have the pyyaml module installed python would complain that ImportError: No module named yaml when I tried to execute my program.

I have ran into this in the past and the solutions posted online vary greatly and usually didn’t work for me. I think I have found the root cause of this and wanted to quickly share it here.

I am running Python 2.7.9 on OS X Mavericks. Pyyaml was installed via pip and works just fine.

The interesting part about this is that when I execute the program by running python program.py everything works fine. If I chmod +x program.py to make it executable and then try to execute it by running ./program.py then this issue begins to arise.

The key issue happens to be the header that tells the OS where to find python.

When you run python from a shell in OS X it is looking to /usr/local/bin/python to find and execute python. You can verify that this is true by running which python in a terminal. When you execute a python program without explicitly calling python then it will look for python in your file header.

The first line of a file typically looks like this #!/usr/bin/python. This was the cause for the descrepancy in my case. Since the python that is called when I say python is different than the python that I defined in my header the module was not able to load.

Pip installs all modules wherever your main python is. The fix for this issue is to change the header from #!/usr/bin/python to #!/usr/bin/env python This way it will look to whatever the default python installation is, and in the event that you are running a virtual environment there will not be any clashing.

This seems like one of those tricky bash gotcha’s that happen sometimes. Using #!/usr/bin/env python by default is a safe bet to make sure you don’t run into these types of issues.

Pipe stuff from bash to your clipboard

I have been using GNU/Linux for over 10 years now and it seems like the more I learn the more I realize how little I know. This week at work we wrote a bad ass bash script that does all sorts of magical stuff and this got me thinking “What else is possible”.

One of my favorite things about GNU and Linux is that whenever you ask yourself “Is it possible to do $X in bash” the answer is usually yes.

For example, I write a lot of these blog posts in vim and it can be a pain to copy the text out to paste back into WP. I wondered if I could just cat everything to my clipboard, and it turns out this is super easy. As usual Stack Overflow has all of the answers.

It turns out there is a program called xclip that lets you take anything from bash and pipe it into your clipboard.

In debian you can install it easily by running:

apt-get install xclip

I set up a bash alias called clip in my ~/.bashrc file that looks like this: alias clip="xclip -selection c"

Now I can cat, grep, ls, echo, etc and pipe it into clip, with the output going directly into my clipboard. For example, the next time you want to email someone a list of all of the chinese hackers that are trying to break into your server you can just do this:

cat /var/log/auth.log | clip

The output will be in your clipboard and you can paste it into your email. This changes everything.

Installing Powerline in Debian

Powerline is an awesome status bar that tells you additional information about various things in bash, vim, and tmux. It comes in handy and makes your terminal look sweet.

It is a little bit of a PITA to install, but it is totally worth it.

I got most of the way there with this very helpful Stack Overflow answer. But I ran into an issue with vim in Debian. I was getting an error telling me that vim needs to be compiled with python support in order for powerline to work.

This seemed silly to me because you should not have to recompile vim in order to use this little plugin. Luckily the solution was easy. There are like 20 different vim packages in debian. The default vim package gives you a bare bones vim install. In order to take advantage of this plugin and other goodies you should install the vim-nox package.

In order to install powerline in debian you should do the following. This will install everything system wide.

  1. Install pre requisites
    sudo apt-get install vim-nox git python-pip
    
  2. Install Powerline
    sudo pip install git+git://github.com/Lokaltog/powerline
    
  3. Install the required fonts
    wget https://github.com/Lokaltog/powerline/raw/develop/font/PowerlineSymbols.otf
    wget https://github.com/Lokaltog/powerline/raw/develop/font/10-powerline-symbols.conf
    sudo mv PowerlineSymbols.otf /usr/share/fonts/
    sudo fc-cache -vf
    sudo mv 10-powerline-symbols.conf /etc/fonts/conf.d/
    
  4. Add the following to your ~/.vimrc
    set rtp+=/usr/local/lib/python2.7/dist-packages/powerline/bindings/vim/
    
    " Always show statusline
    set laststatus=2
    
    " Use 256 colours (Use this setting only if your terminal supports 256 colours)
    set t_Co=256
    
  5. Add the following to your ~/.bashrc
    if [ -f /usr/local/lib/python2.7/dist-packages/powerline/bindings/bash/powerline.sh ]; then
    source /usr/local/lib/python2.7/dist-packages/powerline/bindings/bash/powerline.sh
    fi
    
  6. Add the following to your ~/.tmux.conf
    source /usr/local/lib/python2.7/dist-packages/powerline/bindings/tmux/powerline.conf
    set-option -g default-terminal "screen-256color"
    

Restart your terminal and you should see powerline working now. Open up vim and prepare to be amazed.

Stolen Bank Card

I tend to not think about someone stealing my credit card or identity because that “only happens to careless people”. You know, the kind of people that buy discount cigarettes from some shady company in Hong Kong that does not offer HTTPS on its checkout page.

Well, that naive bubble was burst this weekend when I was in FL. I used my card twice on my first day there. The first was at a gift shop at the hotel. It was a decent hotel so I don’t think that this is the vector where the card was stolen.

The second was at a much shadier store at a mall. This is the place that I think the card number was jacked. I am not sure if the guy was in on it, or if the company he uses to process his payments is in on it, or what. All I know is that the next morning someone went on a shopping spree in Miami beach.

The first charge was for $40.25 at an undisclosed location (my bank cannot even figure out what this charge is for). I think this was just a sample charge to see if I had any money in my account.

The second charge was for $482 at a car rental place in Miami Beach. I guess some douchebag rented a Ferrari or something for the day.

The third (and final charge) was for $398 at Armani Exchange. This was thankfully declined by the bank and this was the charge that prompted my bank to contact me and let me know that something shady was going on with my account.

Thank goodness that my bank (Navy Federal Credit Union) was on top of its stuff and the fraud team quickly got in touch with me and we were able to freeze the account. In addition, thanks to the zero liability policy at the bank I will be able to get all of this money back.

To the people who stole my card, fuck you.

To everyone else who is reading this, please be careful with your card in general and especially when you are traveling. Sadly, there are some shady assholes in the world that want to steal your hard earned money to rent luxury cars and purchase overpriced polos at Armani Exchange.

One great tip that I got from the fraud team was to not use the check card at all since it is directly tied to my checking and savings account and can cause some serious damange. Instead, using the credit card that I have with the bank instead (especially when traveling) will make sure that even if people get the card somehow they cannot do too much damage to the rest of my accounts.

In addition, I think that in this case someone made a copy of my card (since my actual card was not stolen). The new Navy Federal credit cards come with a secure chip that makes duplicating the card pretty difficult. Unfortunately this is not available with the check cards yet, but I am looking forward to it so that these types of issues can be prevented in the future.

In a way I am kind of glad that this happened to me since it opened my eyes a bit and I will be much more careful where I use my card in the future.