3

Hi everyone I'm trying to get Python configured on an OS X laptop and I'm having some trouble. I'm both new to Python and am very unfamiliar with the UNIX terminal. What I'd like to be able to do is to have a directory in my documents folder that would contain python modules and be able to run them from the command line. Currently I have a Python Directory and a chaos.py module inside of it. The full path is /Users/Ben/Documents/Python/chaos.py.

So I followed the steps here and here. I can see that the site-packages for Python 3.4 is in a few spots but I chose this one: '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages' to place the .pth file.

I created a file named Ben.pth in this location with the contents: /Users/Ben/Documents/Python/

Now from my (very limited) understanding that should be all I would need to do for Python to start looking right? So I try to run python3 chaos.py in terminal and I get an error:

/Library/Frameworks/Python.framework/Versions/3.4/Resources/Python.app/Contents/MacOS/Python: can't open file 'chaos.py': [Errno 2] No such file or directory

I'll also try opening IDLE clicking File->Open Module... and try to open it from there and I'll recieve a "module not found" box.

I'm completely stumped, I'm not sure if its a syntax error that I made somewhere (again I don't really know what I'm doing with the UNIX commands) or if I'm just way out in right field. If anyone could help me out, I would greatly appreciate it! Thanks!

Community
  • 1
  • 1
Ben Williams
  • 745
  • 1
  • 5
  • 9
  • You need to cd in the terminal to the right directory and then be able to run the file. – mprat May 16 '15 at 03:23
  • Adding a .pth file in effect adds it as an available module. Thus to run it from python you must tell it that you want to run a module with the -m flag. Something like python -m chaos will cause it to look in site-packages and extensions for a module called chaos – phil_20686 Apr 12 '16 at 10:27

1 Answers1

0

Forget the .pth stuff for now, that's not something you'd normally do. In a unix-ish environment, the typical way you'd run a script would be to change directory:

 cd /Users/Ben/Documents/Python/

and then run the script:

python chaos.py

Another way to do it would be to run the script with an absolute path; you can do this no matter your current working directory:

python /Users/Ben/Documents/Python/chaos.py

Finally, if you've written a utility script you want to be run from anywhere without typing that absolute path all the time, you can do something a little fancier...

Add a 'shebang' line as the first line of your script. It'll go like this:

#!/usr/bin/env python

Get into the directory where your script lives:

cd /Users/Ben/Documents/Python/

Make the script executable:

chmod +x chaos.py

Put a link to the script in a directory on your path... /usr/local/bin/ could be a good choice:

ln -s /Users/Ben/Documents/Python/chaos.py /usr/local/bin/chaos.py

Now you can type chaos.py anywhere on your system and it'll run.

ben author
  • 2,855
  • 2
  • 25
  • 43
  • Awesome that was very helpful. I'm able to run the program from terminal now. But whenever I try to import it into IDLE shell it fails. So I open IDLE (or if I type python3 in terminal to start the python shell the same error occurs) and I type `import chaos.py` and I receive this error. Traceback (most recent call last): File "", line 1, in import chaos ImportError: No module named 'chaos' have I just not set the program up correctly as a module? Am I totally misunderstanding the import command? – Ben Williams May 20 '15 at 14:34
  • I'm not an IDLE user so I can't address its specifics, but if your goal is to import your file as a module, we will go down a different path (no pun intended). First, it's important to know that imports don't deal in filename extensions... the dot indicates `package.module`. So in your example, you were trying to import a module `py` from package `chaos`. What you want is to get your module on your PYTHONPATH... check out https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH ... then you'll be able to `import chaos` – ben author May 20 '15 at 14:42
  • Then, if you do that for a while you'll find that it gets tiresome copying files around or mucking with PYTHONPATH. At that point you'll want to learn how to package your python code so it's installable. Here's a nice tutorial about that: http://www.scotttorborg.com/python-packaging/ – ben author May 20 '15 at 14:45
  • Thanks again for your help, in IDLE i entered `import sys` then `sys.path.append("/Users/Ben/Documents/Python")` and now Python is searching that directory for modules. I can import the chaos.py program just by typing `import chaos`. Exactly what I was looking for! – Ben Williams May 20 '15 at 18:37