从 xcode 宏运行 python 脚本时如何使用我的标准 python 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/364655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:26:26  来源:igfitidea点击:

How do I use my standard python path when running python scripts from xcode macros

pythonxcodemacospath

提问by Lawrence Johnston

I'm trying to run Python scripts using Xcode's User Scripts menu.

我正在尝试使用 Xcode 的用户脚本菜单运行 Python 脚本。

The issue I'm having is that my usual os.sys.path (taken from ~/.profile) does not seem to be imported when running scripts from XCode the way it is when running them at the Terminal (or with IPython). All I get is the default path, which means I can't do things like

我遇到的问题是,从 XCode 运行脚本时,我通常的 os.sys.path(取自 ~/.profile)似乎没有像在终端(或使用 IPython)运行脚本时那样导入。我得到的只是默认路径,这意味着我不能做这样的事情

#!/usr/bin/python
import myScript

myScript.foo()

Where myScript is a module in a folder I've added to my path.

其中 myScript 是我添加到路径中的文件夹中的模块。

I can append a specific path to os.sys.path manually easily enough, but I have to do it in every single script for every single path I want to use modules from

我可以很容易地手动将特定路径附加到 os.sys.path,但我必须在每个脚本中为我想使用模块的每个路径执行此操作

Is there a way to set this up so it uses the same path I use everywhere else?

有没有办法设置它以便它使用我在其他地方使用的相同路径?

EDIT: After looking into things a bit more, it seems like scripts executed from Xcode use a completely different PATH than normal. The path I get by running a script in Xcode is:

编辑:在进一步研究之后,从 Xcode 执行的脚本似乎使用了与正常情况完全不同的 PATH。我通过在 Xcode 中运行脚本得到的路径是:

PATH=/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin

and I'm sure my regular path doesn't have /Developer/usr/bin in it. Does anybody have any idea where this path is coming from?

而且我确定我的常规路径中没有 /Developer/usr/bin。有人知道这条路是从哪里来的吗?

回答by Ted Naleid

On the mac, environment variables in your .profile aren't visible to applications outside of the terminal.

在 Mac 上,.profile 中的环境变量对终端外的应用程序不可见。

If you want an environment variable (like PATH, PYTHONPATH, etc) to be available to xcode apps, you should add it to a new plist file that you create at ~/.MacOSX/environment.plist.

如果您希望 xcode 应用程序可以使用环境变量(如 PATH、PYTHONPATH 等),您应该将其添加到您在 ~/.MacOSX/environment.plist 创建的新 plist 文件中。

See the EnvironmentVarsdoc on the apple developer website for more details.

有关更多详细信息,请参阅Apple 开发者网站上的EnvironmentVars文档。

回答by Toby White

A quick but hackish way is to have a wrapper script for python.

一种快速但不切实际的方法是为 python 编写一个包装脚本。

cat > $HOME/bin/mypython << EOF
#!/usr/bin/python
import os
os.path = ['/list/of/paths/you/want']
EOF

and then start all your XCode scripts with

然后使用以下命令启动所有 XCode 脚本

#!/Users/you/bin/mypython

回答by Charlie Martin

Just add the paths to sys,path.

只需将路径添加到 sys,path。

>>> import sys
>>> sys.path
['', ... lots of stuff deleted....]
>>> for i in sys.path:
...     print i
... 

/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload
/Library/Python/2.5/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC
>>> sys.path.append("/Users/crm/lib")
>>> for i in sys.path:
...     print i
... 

/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload
/Library/Python/2.5/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC
/Users/crm/lib
>>> 

回答by rh0dium

I tend to use pthfiles. From the docs.

我倾向于使用pth文件。从文档。

The most convenient way is to add a path configuration file to a directory that's already on Python's path, usually to the .../site-packages/ directory. Path configuration files have an extension of .pth, and each line must contain a single path that will be appended to sys.path. (Because the new paths are appended to sys.path, modules in the added directories will not override standard modules. This means you can't use this mechanism for installing fixed versions of standard modules.)

最方便的方法是将路径配置文件添加到已经在 Python 路径上的目录中,通常是添加到 .../site-packages/ 目录中。路径配置文件的扩展名为 .pth,每行必须包含一个将附加到 sys.path 的路径。(因为新路径附加到 sys.path,添加目录中的模块不会覆盖标准模块。这意味着您不能使用此机制来安装标准模块的固定版本。)

So the simplest thing to do is to do the following:

因此,最简单的方法是执行以下操作:

echo "/some/path/I/want/to/add" > /Library/Python/2.5/site-packages/custom.pth

HTH

HTH

回答by user3.1415927

Not sure if Xcode counts as launching the script through Finder or not, but if it does, apparently Finder doesn't read .profileor .cshrcfiles when it starts for a user the way Terminal does.

不知道的Xcode算作通过推出Finder或没有剧本,但如果这样做,显然搜索不读.profile.cshrc当它开始为用户终端的方式做文件。

If your question is unanswered still, check out Apple's knowledge base: QA1067and set up your environment using the plist.

如果您的问题仍未得到解答,请查看 Apple 的知识库:QA1067并使用plist.

回答by user3.1415927

Forgive me if my answer seems ignorant, I'm not totally familiar with Mac and I also may have misunderstood your question.

如果我的回答看起来很无知,请原谅我,我对 Mac 并不完全熟悉,而且我也可能误解了您的问题。

On Windows and Linux, when I want to refer to a script I've written, I set the PYTHONPATH environment variable. It is what os.sys.path gets its values from, if I remember correctly.

在 Windows 和 Linux 上,当我想参考我编写的脚本时,我设置了 PYTHONPATH 环境变量。如果我没记错的话,这就是 os.sys.path 的值。

Let's say myScript.py is in /Somewhere. Set PYTHONPATH to:

假设 myScript.py 在 /Somewhere 中。将 PYTHONPATH 设置为:

PYTHONPATH = /Somewhere

Now you should be able to "import myScript".

现在您应该能够“导入 myScript”。

If you start doing sub-folders as python packages, look into usage of init.py files in each folder.

如果您开始将子文件夹作为 python 包进行处理,请查看每个文件夹中init.py 文件的使用情况。

If you plan on re-using this and other scripts all the time, you should leave PYTHONPATH set as an environment variable.

如果您计划一直重复使用此脚本和其他脚本,则应将 PYTHONPATH 设置为环境变量。