Python模块在终端中导入,但不是通过Unix Shell导入

Python模块在终端中导入,但不是通过Unix Shell导入,第1张

概述我刚开始使用Python并需要一些指导.我正在使用Mac,但我安装了python.org版本的python以及最初的Apple安装.我正在尝试加载一些第三方模块.当我在IDLE或终端中运行脚本时,一切正常.当我尝试将其作为CRON作业运行时,我收到错误消息,说它无法找到第三方模块.经过一番探讨之后,我一直认为这是PYTHONPATH / sys.path.我

我刚开始使用Python并需要一些指导.

我正在使用Mac,但我安装了python.org版本的python以及最初的Apple安装.

我正在尝试加载一些第三方模块.当我在IDLE或终端中运行脚本时,一切正常.当我尝试将其作为CRON作业运行时,我收到错误消息,说它无法找到第三方模块.

经过一番探讨之后,我一直认为这是PYTHONPATH / sys.path.我创建了一个测试脚本来向我展示我的路径.那么,为什么我在通过终端窗口而不是直接作为shell脚本时获得不同的路径?

我的两个问题是:

>他们为什么不同?
>如何获得直接shell进程来查找第三方模块?

这是我在终端中运行时的sys.path输出:

['/library/Scripts','/library/Scripts/$','/System/library/Frameworks/Python.framework/Versions/2.7/lib/python2.7','/library/Frameworks/Python.framework/Versions/7.3/lib/python27.zip','/library/Frameworks/Python.framework/Versions/7.3/lib/python2.7','/library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/plat-darwin','/library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/plat-mac','/library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/plat-mac/lib-scriptpackages','/library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-tk','/library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-old','/library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/lib-dynload','/library/Python/2.7/site-packages']

这是我在shell中运行它时的sys.path输出(在这种情况下,我在Applescript中使用“do shell script”步骤.

"['/library/Scripts','/System/library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip','/System/library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin','/System/library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac','/System/library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages','/System/library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python','/System/library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk','/System/library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old','/System/library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload','/System/library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC','/library/Python/2.7/site-packages']"
最佳答案

Why are they different?

因为您正在运行两个不同的Python解释器,并且它们具有不同的默认sys.path值.

这是故意的 – 它允许您修改Python.org安装的标准(或站点)库,而不会破坏系统安装(或任何依赖于该安装的Apple提供的工具).

我猜你在终端中运行python时,你得到/usr/local/bin / python(你可以通过键入哪个python来检查),这可能是/library/Frameworks/Python.framework的链接/Versions/7.3/bin/python(或者可能是该框架内某处的Python.app/Contents/MacOS/python).来自python.org的Python被编译为围绕/library/Frameworks/Python.framework/Versions/7.3/设置sys.path.

同时,当您通过Launch Services运行python时,您可能正在获取/usr/bin/python,这是(希望)内置的垫片/System/library/Frameworks/Python.framework/Versions/2.7/bin/ python,编译为围绕/System/library/Frameworks/Python.framework/Versions/2.7/设置sys.path.

python在这些情况下执行不同的事情的原因是/usr/local/bin根本不在您的登录PATH上,而是通过〜/ .profile(或其他)文件中的一行添加到路径的头部.该文件由终端中的shell会话读取,但不是由Launch Services会话读取.

如果你想在所有环境中使用相同的Python,你可以显式运行/usr/local/bin / python或/usr/bin/python而不是python. (你也可以将/usr/local/bin放到你的登录路径上,但这是一个非常糟糕的主意,或者从你的〜/ .profile中获取它,但这有点不方便.)

作为旁注,这表明您的python.org安装非常破碎:

/library/Frameworks/Python.framework/Versions/7.3/lib/python27.zip

没有Python 7.3.我强烈考虑卸载并重新安装python.org构建.

或者,更简单地说,卸载它并将其卸载,只需使用内置的Python 2.7.如果您需要多个环境,请使用virtualenv而不是多个安装.例如,如果Apple给你2.7.2并且你真的需要2.7.3,那么你需要另一个装置;否则,你只是让事情变得更难,没有任何好处.

总结

以上是内存溢出为你收集整理的Python模块在终端中导入,但不是通过Unix Shell导入全部内容,希望文章能够帮你解决Python模块在终端中导入,但不是通过Unix Shell导入所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1205466.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存