Brandon
Brandon

Reputation: 3130

Issue with my Python environment

Brand new to Python, coming from Ruby.

I have a script that works perfectly if I run it form ipython or ipython qtconsole. I then tried to turn it into an executable script -- threw #!/usr/bin/env python at the top.

Running the script throws an error:

$ ./script/myscript.py Traceback (most recent call last): File "./script/myscript.py", line 6, in <module> import yaml ImportError: No module named yaml

Obviously there's something wrong with how python is loading modules (as it works perfectly fine from the REPL) but I have no idea how to fix it.

Thanks!

Upvotes: 0

Views: 58

Answers (2)

James King
James King

Reputation: 6365

ipython must be pointing at a different version of python than what is in PYTHONPATH.

You can find out by looking at cat /usr/local/bin/ipython.

Look at

ipython reads wrong python version

Upvotes: 1

MattDMo
MattDMo

Reputation: 102862

What is likely happening is you have more than one version of Python installed on your system, and the yaml module is only installed in one of them. When you run ipython it's using one version, but your script's shebang line is finding another version. Run

head `which ipython` 

and see if it matches up to the result of which python (I'm betting it won't). Once you know the path to the python binary being used by ipython, you can specifically define it in your script's shebang line.

As a long-term fix, edit your $PATH variable and put the directory containing your desired version of Python ahead of the directory shown by which python, so that you can continue to use #!/usr/bin/env python as a shebang.

Upvotes: 3

Related Questions