Python:Pyinstaller在mac上的当前目录问题
我创建了一个小脚本,希望在Mac和Windows上作为可执行文件运行 我使用Python:Pyinstaller在mac上的当前目录问题,python,operating-system,directory,executable,pyinstaller,Python,Operating System,Directory,Executable,Pyinstaller,我创建了一个小脚本,希望在Mac和Windows上作为可执行文件运行 我使用--onefile将可执行文件创建为一个文件,并希望使用与可执行文件位于同一目录中的文件 在windowsos中,getcwd()在使用pyinstaller后工作正常,但在mac上它会恢复为基本路径: > /Users/User Traceback (most recent call last): File "test.py", line 93, in <module> FileNotFoundE
--onefile
将可执行文件创建为一个文件,并希望使用与可执行文件位于同一目录中的文件
在windowsos中,getcwd()
在使用pyinstaller
后工作正常,但在mac上它会恢复为基本路径:
> /Users/User
Traceback (most recent call last):
File "test.py", line 93, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/Users/user/Invoices/'
[62121] Failed to execute script test
logout
/Users/User
回溯(最近一次呼叫最后一次):
文件“test.py”,第93行,在
FileNotFoundError:[Errno 2]没有这样的文件或目录:'/Users/user/Invoices/'
[62121]无法执行脚本测试
注销
当我将它作为.py
文件运行时,它也会在mac上获得正确的目录
我已尝试将os.getcwd()
更改为os.path.realpath(\uu文件\uu)
,但在使用pyinstaller转换时,它仍然给出了错误的路径
我希望能够在mac上移动可执行文件,并使用它所在的任何目录。事实证明,以下方法可以工作:
dir_path = slash.join(sys.argv[0].split(slash)[:-1])
这仅在mac上使用可执行文件时有效。在windows上,我仍然使用os.getcwd,在运行python脚本时也是如此。我在PyInstaller 3.6和macOS Catalina上遇到了这个问题。但是,当我尝试您的解决方案时,出现以下错误:
未定义名称“slash”
。我用os.path.sep
替换slash
(不管是什么):os.path.sep.join(sys.argv[0].split(os.path.sep)[:-1])