Trying use a .pth file to add a path in documents folder on Mac

僤鯓⒐⒋嵵緔 提交于 2019-12-10 10:44:40

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/30271466/trying-use-a-pth-file-to-add-a-path-in-documents-folder-on-mac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!