Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:Pyinstaller在mac上的当前目录问题_Python_Operating System_Directory_Executable_Pyinstaller - Fatal编程技术网

Python:Pyinstaller在mac上的当前目录问题

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

我创建了一个小脚本,希望在Mac和Windows上作为可执行文件运行

我使用
--onefile
将可执行文件创建为一个文件,并希望使用与可执行文件位于同一目录中的文件

在windows
os中,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])