Matplotlib“;无法确定主目录";在MSYS2/MinGW中使用PyInstaller时出现问题

Matplotlib“;无法确定主目录";在MSYS2/MinGW中使用PyInstaller时出现问题,matplotlib,gtk,mingw,pyinstaller,msys2,Matplotlib,Gtk,Mingw,Pyinstaller,Msys2,这比这篇文章的标题所暗示的要复杂一些。问题源于我有一个用C编写的大型GTK应用程序,它生成了一个Python matplotlib进程,该进程使用Python GTK库嵌入到GTK窗口中。matplotlib脚本使用PyInstaller编译成可执行文件。在Linux环境(Ubuntu 16.04/18.04)中,一切都可以正常工作,但在尝试使用MSYS2/MinGW为Windows编译应用程序时,情况并非如此。在Windows中编译应用程序时,绘图失败,出现以下错误: Traceback (m

这比这篇文章的标题所暗示的要复杂一些。问题源于我有一个用C编写的大型GTK应用程序,它生成了一个Python matplotlib进程,该进程使用Python GTK库嵌入到GTK窗口中。matplotlib脚本使用PyInstaller编译成可执行文件。在Linux环境(Ubuntu 16.04/18.04)中,一切都可以正常工作,但在尝试使用MSYS2/MinGW为Windows编译应用程序时,情况并非如此。在Windows中编译应用程序时,绘图失败,出现以下错误:

Traceback (most recent call last):
  File "plotter.py", line 6, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "PyInstaller/loader/pyimod03_importers.py", line 531, in exec_module
  File "matplotlib/__init__.py", line 921, in <module>
  File "matplotlib/__init__.py", line 602, in matplotlib_fname
  File "matplotlib/__init__.py", line 599, in gen_candidates
  File "matplotlib/__init__.py", line 239, in wrapper
  File "matplotlib/__init__.py", line 502, in get_configdir
  File "matplotlib/__init__.py", line 444, in _get_xdg_config_dir
  File "pathlib.py", line 1104, in home
  File "pathlib.py", line 267, in gethomedir
RuntimeError: Can't determine home directory
问题在于我不确定到底是什么解决了这个问题,无论发生了什么,我都无法在我的实际GTK程序中重现相同的解决方案。自述文件中概述了我提出的问题,为了更清楚,我将在这里发布这些问题:

(1) Download and install MSYS2 from "https://www.msys2.org/".

(2) This will install both MSYS2 and MinGW to the msys2 directory on your local disk. Go into the msys2 folder and
open a MinGW64 shell as administrator.    

(3) In the MinGW shell, type in the following commands to install the required packages using the Pacman package manager:

    pacman -Syu     (NOTE: You will have to perform this call and close the shell several times for it to complete!)
    pacman -S mingw-w64-x86_64-python-pip
    pacman -S mingw-w64-x86_64-python-matplotlib
    pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-gtk3 mingw-w64-x86_64-python3 mingw-w64-x86_64-python3-gobject
    pacman -S git make nano pkg-config gcc

(4) Clone the GitHub repository with the example showing the error in question:

    mkdir testing
    cd testing
    git clone https://github.com/LeighKorbel/pyinstaller_msys2_issue.git
    cd pyinstaller_msys2_issue

(5) Download PyInstaller using Python's pip package manager:

    pip install pyinstaller

(6) Now we are ready to start compiling. The first thing we need to do is run PyInstaller on the python script. This
    script is just a simple matplotlib plot that is embedded within a GTK window. Run the following commands:

    cd gtk_simple_plot
    pyinstaller plot.py

(7) After PyInstaller finishes, an executable file will now be located in "/dist/plot". This can be run from the 
    command line by:

    ./dist/plot/plot.exe

(8) Now that we have verified that this works, we want to compile a very basic GTK application in C that only has a 
    button that will spawn the script using GLib's g_spawn() function. However, this needs to be done in a MSYS2 shell
    because MinGW does not have the system headers needed in it. So you need to open an MSYS2 shell now as an administrator.
    After this is done, we will need to set an environment variable for pkg_config, which is used by GTK. I would
    recommend adding the following line to the .bashrc file and then closing and reopening the MSYS2 shell, but it is
    not required (you will just need to remember to export again in the case that you close and reopen the shell). Once
    the MSYS2 shell is open type in:

    nano .bashrc

(9) Scroll to the bottom of the file and type in the following:

    export PKG_CONFIG_PATH="/mingw64/lib/pkgconfig:$PKG_CONFIG_PATH"

(10) Now exit and save by pressing "Ctrl-x", then "y", then "enter". Close and reopen the shell. Now enter the following commands:

    cd testing/call_gtk_plot_from_C
    make

(11) The GTK program will now be compiled and can be executed by running:

    ./launch.exe

(12) Press the button on the UI to launch the PyInstaller compiled matplotlib script. The following error should
     appear in the MSYS2 shell:

    Traceback (most recent call last):
      File "plotter.py", line 6, in <module>
      File "<frozen importlib._bootstrap>", line 991, in _find_and_load
      File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
      File "PyInstaller/loader/pyimod03_importers.py", line 531, in exec_module
      File "matplotlib/__init__.py", line 921, in <module>
      File "matplotlib/__init__.py", line 602, in matplotlib_fname
      File "matplotlib/__init__.py", line 599, in gen_candidates
      File "matplotlib/__init__.py", line 239, in wrapper
      File "matplotlib/__init__.py", line 502, in get_configdir
      File "matplotlib/__init__.py", line 444, in _get_xdg_config_dir
      File "pathlib.py", line 1104, in home
      File "pathlib.py", line 267, in gethomedir
    RuntimeError: Can't determine home directory

********************************************************************************************************************
QUESTION 1: WHAT IS THE CAUSE OF THIS ERROR? IS THIS FROM THE DUAL USE OF MinGW FOR PYINSTALLER AND MSYS2 FOR MAKE?
            IS IT A PATH ISSUE?
********************************************************************************************************************

(13) This error can be eliminated by performing the following: In the "call_gtk_plot_from_C" folder, edit both the 
     makefile and the launch.c code (in src/) to remove all instances of "../gtk_simple_plot/" and then copy and move 
     the plot.py script from the gtk_simple_plot directory into the call_gtk_plot_from_C directory. From a MinGW shell
     run make.

     NOW IT WILL MAKE EVERYTHING, EVEN THE C CODE.

(14) Run the program:

     ./launch.exe

     When you run this time it errors out with a gdk_pixbuf error. For some reason, in /dist/plot/lib/gdk-pixbuf there
     is no longer the folder "loaders" which does exist on the previous run of PyInstaller. Copy it from the dist/
     folder in gtk_simple_plot over to the dist/ folder in call_gtk_plot_from_C. Run the program again.

     NOW IT WORKS WITHOUT THE PREVIOUS HOME DIRECTORY ERROR.

********************************************************************************************************************
QUESTION 2: WHAT CAUSED IT TO WORK NOW? WAS IT THE FULL COMPILE IN MinGW? IS THERE A PATH THAT IS DIFFERENT NOW WITH
            THE PYTHON SCRIPT BEING MOVED INTO THE NEW DIRECTORY? WHY WAS THE PIXBUF MISSING?
********************************************************************************************************************

********************************************************************************************************************
QUESTION 3: WHY DOES THE C CODE COMPILE IN MinGW NOW? IT DOES NOT CONTAIN THE SYSTEM HEADERS REQUIRED FOR IT COMPILE?
********************************************************************************************************************
(1)从下载并安装MSYS2“https://www.msys2.org/".
(2) 这将把MSYS2和MinGW安装到本地磁盘上的MSYS2目录中。进入msys2文件夹并
以管理员身份打开MinGW64 shell。
(3) 在MinGW shell中,键入以下命令以使用Pacman软件包管理器安装所需的软件包:
pacman-Syu(注意:您必须执行此调用并关闭shell数次才能完成!)
pacman-S mingw-w64-x86_64-python-pip
pacman-S mingw-w64-x86_64-python-matplotlib
pacman-S mingw-w64-x86_64-gcc mingw-w64-x86_64-gtk3 mingw-w64-x86_64-python3 mingw-w64-x86_64-python3-gobject
pacman-sgit制作nano-pkg配置gcc
(4) 克隆GitHub存储库,示例显示有问题的错误:
mkdir测试
cd测试
git克隆https://github.com/LeighKorbel/pyinstaller_msys2_issue.git
cd pyinstaller\u msys2\u问题
(5) 使用Python的pip包管理器下载PyInstaller:
pip安装程序
(6) 现在我们可以开始编译了。我们需要做的第一件事是在python脚本上运行PyInstaller。这
脚本只是嵌入GTK窗口中的一个简单matplotlib绘图。运行以下命令:
cd gtk_简单图
pyinstaller plot.py
(7) PyInstaller完成后,可执行文件现在将位于“/dist/plot”中。这可以从
命令行执行者:
./dist/plot/plot.exe
(8) 现在我们已经验证了这一点,我们想用C编译一个非常基本的GTK应用程序,它只有一个
按钮,该按钮将使用GLib的g_spawn()函数生成脚本。但是,这需要在MSYS2 shell中完成
因为MinGW中没有所需的系统头。因此,您现在需要以管理员身份打开MSYS2 shell。
完成后,我们需要为pkg_config设置一个环境变量,GTK使用该变量。我会的
建议在.bashrc文件中添加以下行,然后关闭并重新打开MSYS2 shell,但事实并非如此
不需要(如果关闭并重新打开shell,您只需要记住再次导出)。一旦
MSYS2外壳在以下情况下为开放式:
nano.bashrc
(9) 滚动至文件底部并键入以下内容:
export PKG_CONFIG_PATH=“/mingw64/lib/pkgconfig:$PKG_CONFIG_PATH”
(10) 现在按“Ctrl-x”,然后按“y”,然后按“enter”退出并保存。关闭并重新打开外壳。现在输入以下命令:
cd测试/呼叫来自C的gtk图
制作
(11) 现在将编译GTK程序,并可通过运行以下程序执行:
/launch.exe
(12) 按UI上的按钮启动PyInstaller编译的matplotlib脚本。应注意以下错误
出现在MSYS2外壳中:
回溯(最近一次呼叫最后一次):
文件“plotter.py”,第6行,在
文件“”,第991行,在“查找”和“加载”中
文件“”,第975行,在“查找”和“加载”中解锁
文件“”,第671行,在\u加载\u解锁
exec_模块中第531行的文件“PyInstaller/loader/pyimod03_importers.py”
文件“matplotlib/_init__uu.py”,第921行,在
matplotlib\u fname中第602行的文件“matplotlib/\uuuuu init\uuuuuu.py”
第599行gen_候选文件中的文件“matplotlib/_init__uuuuuuuuuuuuuuuuuu.py”
包装器中第239行的文件“matplotlib/_init__uuuuuuuuuuu.py”
get_configdir中第502行的文件“matplotlib/_init__uuuu.py”
文件“matplotlib/uuuu init_uuu.py”,第444行,在get_xdg_config_dir中
文件“pathlib.py”,第1104行,在home中
gethomedir中第267行的文件“pathlib.py”
运行时错误:无法确定主目录
********************************************************************************************************************
问题1:这一错误的原因是什么?这是因为MinGW用于PYINSTALLER和MSYS2用于MAKE的双重用途吗?
这是一个路径问题吗?
********************************************************************************************************************
(13) 通过执行以下操作可以消除此错误:在“call_gtk_plot_from_C”文件夹中,编辑
makefile和launch.c代码(src/)删除“./gtk_simple_plot/”的所有实例,然后复制并移动
将plot.py脚本从gtk_simple_plot目录转换到call_gtk_plot_from_C目录。从一个明壳
跑来跑去。
现在,它将使一切,甚至C代码。
(14) 运行程序:
/launch.exe
当你这次运行时,它会出错,出现gdk_pixbuf错误。出于某种原因,在/dist/plot/lib/gdk pixbuf中
不再是上次运行PyInstaller时确实存在的文件夹“loaders”。从dist复制它/
gtk_simple_plot中的文件夹转到call_gtk_plot_from_C中的dist/文件夹。再次运行该程序。
现在,它的工作没有以前的主目录错误。
********************************************************************************************************************
问题2:是什么导致它现在工作?这是MinGW的完整编译吗?有没有一条与现在不同的道路
(1) Download and install MSYS2 from "https://www.msys2.org/".

(2) This will install both MSYS2 and MinGW to the msys2 directory on your local disk. Go into the msys2 folder and
open a MinGW64 shell as administrator.    

(3) In the MinGW shell, type in the following commands to install the required packages using the Pacman package manager:

    pacman -Syu     (NOTE: You will have to perform this call and close the shell several times for it to complete!)
    pacman -S mingw-w64-x86_64-python-pip
    pacman -S mingw-w64-x86_64-python-matplotlib
    pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-gtk3 mingw-w64-x86_64-python3 mingw-w64-x86_64-python3-gobject
    pacman -S git make nano pkg-config gcc

(4) Clone the GitHub repository with the example showing the error in question:

    mkdir testing
    cd testing
    git clone https://github.com/LeighKorbel/pyinstaller_msys2_issue.git
    cd pyinstaller_msys2_issue

(5) Download PyInstaller using Python's pip package manager:

    pip install pyinstaller

(6) Now we are ready to start compiling. The first thing we need to do is run PyInstaller on the python script. This
    script is just a simple matplotlib plot that is embedded within a GTK window. Run the following commands:

    cd gtk_simple_plot
    pyinstaller plot.py

(7) After PyInstaller finishes, an executable file will now be located in "/dist/plot". This can be run from the 
    command line by:

    ./dist/plot/plot.exe

(8) Now that we have verified that this works, we want to compile a very basic GTK application in C that only has a 
    button that will spawn the script using GLib's g_spawn() function. However, this needs to be done in a MSYS2 shell
    because MinGW does not have the system headers needed in it. So you need to open an MSYS2 shell now as an administrator.
    After this is done, we will need to set an environment variable for pkg_config, which is used by GTK. I would
    recommend adding the following line to the .bashrc file and then closing and reopening the MSYS2 shell, but it is
    not required (you will just need to remember to export again in the case that you close and reopen the shell). Once
    the MSYS2 shell is open type in:

    nano .bashrc

(9) Scroll to the bottom of the file and type in the following:

    export PKG_CONFIG_PATH="/mingw64/lib/pkgconfig:$PKG_CONFIG_PATH"

(10) Now exit and save by pressing "Ctrl-x", then "y", then "enter". Close and reopen the shell. Now enter the following commands:

    cd testing/call_gtk_plot_from_C
    make

(11) The GTK program will now be compiled and can be executed by running:

    ./launch.exe

(12) Press the button on the UI to launch the PyInstaller compiled matplotlib script. The following error should
     appear in the MSYS2 shell:

    Traceback (most recent call last):
      File "plotter.py", line 6, in <module>
      File "<frozen importlib._bootstrap>", line 991, in _find_and_load
      File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
      File "PyInstaller/loader/pyimod03_importers.py", line 531, in exec_module
      File "matplotlib/__init__.py", line 921, in <module>
      File "matplotlib/__init__.py", line 602, in matplotlib_fname
      File "matplotlib/__init__.py", line 599, in gen_candidates
      File "matplotlib/__init__.py", line 239, in wrapper
      File "matplotlib/__init__.py", line 502, in get_configdir
      File "matplotlib/__init__.py", line 444, in _get_xdg_config_dir
      File "pathlib.py", line 1104, in home
      File "pathlib.py", line 267, in gethomedir
    RuntimeError: Can't determine home directory

********************************************************************************************************************
QUESTION 1: WHAT IS THE CAUSE OF THIS ERROR? IS THIS FROM THE DUAL USE OF MinGW FOR PYINSTALLER AND MSYS2 FOR MAKE?
            IS IT A PATH ISSUE?
********************************************************************************************************************

(13) This error can be eliminated by performing the following: In the "call_gtk_plot_from_C" folder, edit both the 
     makefile and the launch.c code (in src/) to remove all instances of "../gtk_simple_plot/" and then copy and move 
     the plot.py script from the gtk_simple_plot directory into the call_gtk_plot_from_C directory. From a MinGW shell
     run make.

     NOW IT WILL MAKE EVERYTHING, EVEN THE C CODE.

(14) Run the program:

     ./launch.exe

     When you run this time it errors out with a gdk_pixbuf error. For some reason, in /dist/plot/lib/gdk-pixbuf there
     is no longer the folder "loaders" which does exist on the previous run of PyInstaller. Copy it from the dist/
     folder in gtk_simple_plot over to the dist/ folder in call_gtk_plot_from_C. Run the program again.

     NOW IT WORKS WITHOUT THE PREVIOUS HOME DIRECTORY ERROR.

********************************************************************************************************************
QUESTION 2: WHAT CAUSED IT TO WORK NOW? WAS IT THE FULL COMPILE IN MinGW? IS THERE A PATH THAT IS DIFFERENT NOW WITH
            THE PYTHON SCRIPT BEING MOVED INTO THE NEW DIRECTORY? WHY WAS THE PIXBUF MISSING?
********************************************************************************************************************

********************************************************************************************************************
QUESTION 3: WHY DOES THE C CODE COMPILE IN MinGW NOW? IT DOES NOT CONTAIN THE SYSTEM HEADERS REQUIRED FOR IT COMPILE?
********************************************************************************************************************