尝试使用.pth文件在Mac上的documents文件夹中添加路径 [英] Trying use a .pth file to add a path in documents folder on Mac
问题描述
大家好,我试图在OS X笔记本电脑上配置Python,但遇到了一些麻烦.我既不熟悉Python,也不熟悉UNIX终端.我想做的是在我的documents文件夹中有一个目录,其中包含python模块,并能够从命令行运行它们.目前,我内部有一个Python目录和一个chaos.py
模块.完整路径为/Users/Ben/Documents/Python/chaos.py
.
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
.
所以我遵循了此处和此处.我可以看到Python 3.4的站点包位于几个位置,但我选择了以下位置:'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages'
放置.pth
文件.
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.
我在此位置创建了一个名为Ben.pth
的文件,其内容为:/Users/Ben/Documents/Python/
I created a file named Ben.pth
in this location with the contents: /Users/Ben/Documents/Python/
现在,根据我的(非常有限的)理解,这应该是Python开始看起来正确所需要做的一切?所以我尝试在终端中运行python3 chaos.py
,但出现错误:
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
我还将尝试通过单击File-> Open Module ...来打开IDLE,然后尝试从那里打开它,然后我将收到一个找不到模块"框.
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.
我完全迷住了,我不确定这是我在某个地方犯的语法错误(再次我真的不知道我在使用UNIX命令做什么),或者我只是走出一条路正确的领域.如果有人可以帮助我,我将不胜感激!谢谢!
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!
推荐答案
暂时忘记.pth内容,那不是您通常要做的.在unix-ish环境中,运行脚本的典型方法是更改目录:
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/
然后运行脚本:
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...
添加"shebang"行作为脚本的第一行.它会像这样:
Add a 'shebang' line as the first line of your script. It'll go like this:
#!/usr/bin/env python
进入脚本所在的目录:
cd /Users/Ben/Documents/Python/
使脚本可执行:
chmod +x chaos.py
在路径上的目录中放置脚本的链接... /usr/local/bin/
可能是一个不错的选择:
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
现在,您可以在系统上的任何地方键入chaos.py
,它将运行.
Now you can type chaos.py
anywhere on your system and it'll run.
这篇关于尝试使用.pth文件在Mac上的documents文件夹中添加路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!