首页 > 解决方案 > .profile、.bash.profile、.profile.pysave 中的 Python $PATH 是否应该相同?

问题描述

在 Mac OS XI 上安装了几个 Python 版本,用于 afdk(字体修改模块)。有人建议我删除它们并重新开始,我已经这样做了。

查看 .profile、.bash.profile、.profile.pysave 文件,我看到我安装的所有以前的版本都被记录下来。为了只使用最新的 Python 版本,我应该通过删除旧版本来破解这些 .bash 文件,并且只保留当前 Python 版本的 $PATH 吗?

$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/adrianshome/bin/FDK/Tools/osx

谢谢,阿德里安

所以这里是下面提到的 .bash 文件。

。轮廓:

##
# DELUXE-USR-LOCAL-BIN-INSERT
# (do not remove this comment)
##
echo $PATH | grep -q -s "/usr/local/bin"
if [ $? -eq 1 ] ; then
    PATH=$PATH:/usr/local/bin
    export PATH
fi

# Setting PATH for MacPython 2.5
# The orginal version is saved in .profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"

# Setting PATH for Python 3.4
# The orginal version is saved in .profile.pysave
# Initialization for FDK command line tools.Sun Oct 14 12:28:07 2018
FDK_EXE="/Users/adrianshome/bin/FDK/Tools/osx"
PATH=${PATH}:"/Users/adrianshome/bin/FDK/Tools/osx"
export PATH
export FDK_EXE

.profile.pysave:

##
# DELUXE-USR-LOCAL-BIN-INSERT
# (do not remove this comment)
##
echo $PATH | grep -q -s "/usr/local/bin"
if [ $? -eq 1 ] ; then
    PATH=$PATH:/usr/local/bin
    export PATH
fi

# Setting PATH for MacPython 2.5
# The orginal version is saved in .profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"
export PATH
# Initialization for FDK command line tools.Wed May 21 12:21:59 2008
FDK_EXE="/Users/adrianwi/bin/FDK/Tools/osx"
PATH=${PATH}:"/Users/adrianwi/bin/FDK/Tools/osx"

.bash.profile:

# Initialization for FDK command line tools.Fri Apr 10 14:12:30 2015

# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"

# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"

# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"

# Setting PATH for Python 2.7

这是安装 Python3 和 AFDK 模块的结果...

Last login: Sun Oct 14 12:50:11 on ttys001
Adrians-iMac-2:~ adrianshome$ pip3 install --user --upgrade afdko;
Collecting afdko
  Downloading https://files.pythonhosted.org/packages/b6/d2/0ba8ccfe42b0f88b0cc2819b73d93d4ad6240285a8d9a6a1570f39b1e613/afdko-2.8.2-py2.py3-none-macosx_10_6_intel.whl (2.4MB)
    100% |████████████████████████████████| 2.4MB 543kB/s 
Collecting ufoLib~=2.3.1 (from afdko)
  Downloading https://files.pythonhosted.org/packages/b7/72/e053c7f9adc52ee7f4ac163cebef4c8d39732a79a02f9abcfdcfbca35785/ufoLib-2.3.2-py2.py3-none-any.whl (109kB)
    100% |████████████████████████████████| 112kB 525kB/s 
Collecting ufonormalizer~=0.3.5 (from afdko)
  Downloading https://files.pythonhosted.org/packages/e6/f3/cdf8d7616cde052b07f3fb83eec34a4a534965df71968cbef9f3895a31c3/ufonormalizer-0.3.5-py2.py3-none-any.whl
Collecting defcon~=0.5.3 (from afdko)
  Downloading https://files.pythonhosted.org/packages/e5/e5/7491cd6b2f5ec7ef96804f31b8c5fd4d8030d2ff7f331fc2c179ff196bfa/defcon-0.5.3-py2.py3-none-any.whl (212kB)
    100% |████████████████████████████████| 215kB 1.6MB/s 
Collecting booleanOperations~=0.8.0 (from afdko)
  Downloading https://files.pythonhosted.org/packages/bd/98/4c43398f43b023803609ac4ab46b02f10936ba501c8e53e70ee8f21d794d/booleanOperations-0.8.0-py2.py3-none-any.whl
Collecting fontTools~=3.29.1 (from afdko)
  Downloading https://files.pythonhosted.org/packages/ab/68/a583423ac2ca5dd624ca4914ee97307b4b61a83dda5e0451f0242f628f58/fonttools-3.29.1-py2.py3-none-any.whl (612kB)
    100% |████████████████████████████████| 614kB 551kB/s 
Collecting psautohint==1.8.1 (from afdko)
  Downloading https://files.pythonhosted.org/packages/50/95/4583fcdb5cca5efe32c46dfa2bab95fcf2a99f43a8fd201971b5978d4705/psautohint-1.8.1-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (377kB)
    100% |████████████████████████████████| 378kB 486kB/s 
Collecting mutatorMath~=2.1.1 (from afdko)
  Downloading https://files.pythonhosted.org/packages/01/26/6d1583731da1d236f917b6618ac342ad435eb761750f934f1fc1bfd9ae84/MutatorMath-2.1.1-py2.py3-none-any.whl
Collecting fontMath~=0.4.7 (from afdko)
  Downloading https://files.pythonhosted.org/packages/33/b2/6e4d8fd1c0c82f42961dd4e901ecf819e490a64f2abb0a7bdf5082997a4f/fontMath-0.4.7-py2.py3-none-any.whl
Collecting fontPens~=0.1.0 (from afdko)
  Downloading https://files.pythonhosted.org/packages/76/11/0ad531d40c608eb3b798bb94607b10109b4e145142fe68eda40e92b84daf/fontPens-0.1.0-py2.py3-none-any.whl
Collecting pyclipper>=1.0.5 (from booleanOperations~=0.8.0->afdko)
  Downloading https://files.pythonhosted.org/packages/a0/2f/6a75e90bfb924118bd65cc16ccd23df53db697f0df7a4dfa79c6d64913fe/pyclipper-1.1.0.post1-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (275kB)
    100% |████████████████████████████████| 276kB 510kB/s 
Installing collected packages: fontTools, ufoLib, ufonormalizer, defcon, pyclipper, booleanOperations, psautohint, fontMath, mutatorMath, fontPens, afdko
  The scripts fonttools, pyftinspect, pyftmerge, pyftsubset and ttx are installed in '/Users/adrianshome/Library/Python/3.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The script ufonormalizer is installed in '/Users/adrianshome/Library/Python/3.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The script psautohint is installed in '/Users/adrianshome/Library/Python/3.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  The scripts autohint, buildcff2vf, buildmasterotfs, charplot, checkoutlinesufo, comparefamily, digiplot, fontplot, fontplot2, fontsetplot, hintplot, makeinstancesufo, makeotf, otc2otf, otf2otc, stemhist, ttfcomponentizer, ttxn and waterfallplot are installed in '/Users/adrianshome/Library/Python/3.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed afdko-2.8.2 booleanOperations-0.8.0 defcon-0.5.3 fontMath-0.4.7 fontPens-0.1.0 fontTools-3.29.1 mutatorMath-2.1.1 psautohint-1.8.1 pyclipper-1.1.0.post1 ufoLib-2.3.2 ufonormalizer-0.3.5
Adrians-iMac-2:~ adrianshome$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/adrianshome/bin/FDK/Tools/osx
Adrians-iMac-2:~ adrianshome$ 
Adrians-iMac-2:~ adrianshome$ 

更新:11 月 2 日

感谢非常好的解释!我重新安装了 Python3 并执行了“pip3 install --user --upgrade afdko”来获得我需要的字体工具模块。创建了一个虚拟环境。然后再次运行我的代码例程,出现有关“站点包”目录中未找到的模块的错误 - 即使它们在那里:

将 ttx 拖到终端

粘贴代码以在桌面上创建“Dsig fldr”,其中包含 dsig.ttx 文件。

输入'-m'

将“SassoInfProReg.ttf”拖到终端

将“dsig.ttx”拖到终端

返回

有了这个结果(为代码不都是等宽的道歉?)......

(env37) Adrians-iMac-2:Dsig fldr adrianshome$ FLDR="${HOME}/Desktop/Dsig fldr"

(env37) Adrians-iMac-2:Dsig fldr adrianshome$ mkdir -p "$FLDR" (env37) Adrians-iMac-2:Dsig fldr adrianshome$ cd "$FLDR" (env37) Adrians-iMac-2:Dsig fldr adrianshome $猫> dsig.ttx<

00000001 00000000 EOF (env37) Adrians-iMac-2:Dsig fldr adrianshome$ 打开。(env37) Adrians-iMac-2:Dsig fldr adrianshome$ /Library/Python/3.7/bin/ttx -m /Users/adrianshome/Desktop/Dsig\ fldr/SassoInfProReg.ttf /Users/adrianshome/Desktop/Dsig\ fldr/ dsig.ttx

正在将“/Users/adrianshome/Desktop/Dsig fldr/dsig.ttx”编译为“/Users/adrianshome/Desktop/Dsig fldr/dsig.ttf”...正在解析“DSIG”表...错误:发生未处理的异常 Traceback (最近一次通话最后):

主进程中的文件“/Users/adrianshome/Library/Python/3.7/lib/python/site-packages/fontTools/ttx.py”,第 398 行(作业,选项)

文件“/Users/adrianshome/Library/Python/3.7/lib/python/site-packages/fontTools/ttx.py”,第 372 行,正在进行操作(输入、输出、选项)

包装器中的文件“/Users/adrianshome/Library/Python/3.7/lib/python/site-packages/fontTools/misc/loggingTools.py”,第 375 行,返回 func(*args, **kwds)

文件“/Users/adrianshome/Library/Python/3.7/lib/python/site-packages/fontTools/ttx.py”,第 290 行,在 ttCompile ttf.save(输出)

文件“/Users/adrianshome/Library/Python/3.7/lib/python/site-packages/fontTools/ttLib/ttFont.py”,第 173 行,保存 writer_reordersTables = self._save(tmp)

_save self._writeTable(tag, writer, done, tableCache) 中的文件“/Users/adrianshome/Library/Python/3.7/lib/python/site-packages/fontTools/ttLib/ttFont.py”,第 212 行

_writeTable tabledata = self.getTableData(tag) 中的文件“/Users/adrianshome/Library/Python/3.7/lib/python/site-packages/fontTools/ttLib/ttFont.py”,第 633 行

文件“/Users/adrianshome/Library/Python/3.7/lib/python/site-packages/fontTools/ttLib/ttFont.py”,第 651 行,在 getTableData 返回 self.tables[tag].compile(self)

文件“/Users/adrianshome/Library/Python/3.7/lib/python/site-packages/fontTools/ttLib/tables/D_S_I_G_.py”,第 60 行,编译打包 = sstruct.pack(DSIG_HeaderFormat, self)

文件“/Users/adrianshome/Library/Python/3.7/lib/python/site-packages/fontTools/misc/sstruct.py”,第 68 行,在 pack value = obj[name] KeyError: 'ulVersion' (env37) Adrians -iMac-2:Dsig fldr adrianshome$

标签: pythonbashmacosdependencies

解决方案


这取决于您需要使用 Python 做什么,有时您的操作系统(大多数情况下是 Linux)带有预安装的 Python 版本,这是许多内置功能的要求。通过删除或更改系统范围的版本,您可能会对系统的运行方式产生负面影响。我不太确定 OSX 是否属于这种情况,因为我对它的经验有限。

如今,Python 应用程序安装自己的本地 Python 版本(虚拟环境)是相当普遍的做法,他们可以在其中安装自己的依赖项,而不会污染或对其系统范围的版本产生负面影响。

如果您确定没有其他人使用它,那么更改它可能是安全的,但可能首先重命名目录并查看会发生什么(或中断),然后再删除它。

关于您的相关个人资料文件:

.profile.pysave

无需修改它,它只是在您安装 python 时创建的备份,因为它会修改您现有的 .profile 但会在您想回滚时进行备份。

在您的 .profile 文件中,您可以将所有路径组合成一个变量,然后将其导出。例如

PATH=${PATH}:/Library/Frameworks/Python.framework/Versions/Current/bin:/Users/adrianshome/bin/FDK/Tools/osx 
export PATH

.bash.profile

这有点棘手......你想在这里实现什么?

您只想使用最新版本的 python 还是尝试同时使用所有三个版本?

我认为您可能只想拥有一个最新版本的条目。像你一样拥有这三个会相互冲突,并且不会有预期的行为。

PATH 的工作方式是它按照您指定的顺序在您列出的目录中查找可执行文件。所以如果你有这样的路径

PATH=/dir1:/dir2:/dir3

并且在这三个目录中的每一个中,如果您有一个名为“python”的文件(假设它设置为可执行文件并且您拥有适当的权限),那么任何时候您在终端中键入“python”,您实际上都会调用 /dir1/python,因为它是PATH 中的第一个条目。

如您所见,如果您真的想使用 /dir3 中的“python”,这将导致问题。

如果您确实需要使用多个版本的 python,我强烈建议您使用虚拟环境(google virtualenv)。这些将允许您拥有一个独立版本的 python,您可以在需要时“激活”它。

例如,我有两个程序,App1 使用 python 2.7,App2 使用 python 3.7,我有两个虚拟环境。我会像这样激活它们(注意这是一个示例,确切的路径将取决于您创建它的位置)。

$ /Users/adrianshome/py2.7/bin/activate
$ App1

现在您的 python 2.7 处于活动状态并且位于路径的第一位,因此当您键入 python 时,您将获得 python 2.7

同样,您将执行以下操作来运行 App2

$ deactivate (this will undo what you did previously)
$ /Users/adrianshome/py3.7/bin/activate
$ App2

我希望这有帮助!


推荐阅读