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

时间:2022-03-08 20:20:02

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

我正在尝试使用Xcode的User Scripts菜单运行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上运行脚本时,在终端(或使用IPython)运行脚本时,似乎不会导入我通常的os.sys.path(取自〜/ .profile)。我得到的只是默认路径,这意味着我无法做到这样的事情

#!/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。有没有人知道这条路是从哪里来的?

6 个解决方案

#1


5  

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 EnvironmentVars doc on the apple developer website for more details.

有关详细信息,请参阅apple开发人员网站上的EnvironmentVars文档。

#2


1  

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

一个快速而又hackish的方法是为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

#3


1  

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
>>> 

#4


1  

I tend to use pth files. 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

#5


0  

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设置为环境变量。

#6


0  

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

不确定Xcode是否算作通过Finder启动脚本,但如果确实如此,显然当Finder以终端的方式为用户启动时,它不会读取.profile或.cshrc文件。

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

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

#1


5  

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 EnvironmentVars doc on the apple developer website for more details.

有关详细信息,请参阅apple开发人员网站上的EnvironmentVars文档。

#2


1  

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

一个快速而又hackish的方法是为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

#3


1  

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
>>> 

#4


1  

I tend to use pth files. 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

#5


0  

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设置为环境变量。

#6


0  

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

不确定Xcode是否算作通过Finder启动脚本,但如果确实如此,显然当Finder以终端的方式为用户启动时,它不会读取.profile或.cshrc文件。

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

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