Pyinstaller doesn't seem to find the data file

Edit 3: I replaced __file__ with sys.argv[0] when I need to know the location of my script / executable. This is not exactly the same, but in my case it works fine (at least on the executable version ...). Now everything works fine, in single-file mode, using the accepted response function to access resource files!


Edit 2: as shown in the accepted answers, the problem arises from the path resolution in my script; I am trying to use __file__ to get the location of a script so that I can access its resource files. This does not work after packaging, as __file__ returns the file name from Python.dll to the script, so there is always no path and just the file name. So I have to find another trick to make access to resource files; The workaround at the moment is to move the current directory to the executable path.

By the way, this means that ConfigParser should report a problem while accessing the file, and not that the section is missing.

I will update this question with how I resolved this path resolution question.


I have problems with pyinstaller , and since it is the first time I use it, it is sure that I did something wrong.

So here is the problem: pyisntaller runs smoothly on the script I wrote and generates some things in the dist folder. So now I want to execute it to see if everything is fine, and here is what I get:

 C:\Program Files\PyInstaller\pyinstaller-1.5.1>p_tool\dist\p_tool\p_tool.exe -? Traceback (most recent call last): File "<string>", line 104, in <module> File "p_tool\build\pyi.win32\p_tool\outPYZ1.pyz/logging.config", line 76, in f ileConfig File "p_tool\build\pyi.win32\p_tool\outPYZ1.pyz/logging.config", line 112, in _create_formatters File "p_tool\build\pyi.win32\p_tool\outPYZ1.pyz/ConfigParser", line 532, in ge t ConfigParser.NoSectionError: No section: 'formatters' 

My first idea was that the logging.conf file logging.conf missing, so I added it (and some other resource files) to the p_tool.spec file, but this is not better.

Python version: 2.6.6 under WinXP. I am using pyinstaller since I will need it for batch files for a Solaris workstation.

So, did anyone have this problem? The only topic is related to the following question: PyInstaller problem , really close to my problem, but hopelessly it did not receive an answer.


Edit3: Log deletion information was deleted because it is not related to the problem.

+6
source share
3 answers

Firstly, it would be wise to make the file config_file / os.path.exists (config_file) before reading it so that you can make sure where the file is and if python can find it.

As for the actual access to it, then os.path.split(__file__) looks almost correct, but I'm not sure if it works correctly in pyinstaller. The proper way to pack files is to add them to the .spec file, then pyinstaller will load them at compile time and unzip them to $ _MEIPASS2 / at run time. To get the _MEIPASS2 directory in packaged mode and use the local directory in unpack (development) mode, I use this:

 def resource_path(relative): return os.path.join( os.environ.get( "_MEIPASS2", os.path.abspath(".") ), relative ) # in development >>> resource_path("logging.conf") "/home/shish/src/my_app/logging.conf" # in deployment >>> resource_path("logging.conf") "/tmp/_MEI34121/logging.conf" 
+9
source

The ConfigParser.NoSectionError: No section: 'formatters' error message suggests that this is not a missing file, but a file with a missing section that you should be looking for.

0
source

I had a similar problem, but so far have not been able to find an elegant solution. The “hack” that I use got me, said that my project is in '~/project/project_root' , first in the .spec file:

 excluded_sources = TOC([x for x in a.pure if not x[0].startswith('project_root')]) 

Here a is an Analysis object, basically I delete all project files from PYZ , so import is not transferred there, and relative log paths will not be calculated from there. After that, create a Tree object from the project.

 my_project_tree = Tree('~/project') 

Then add this tree to the TOC list that is passed to COLLECT, therefore:

 COLLECT( exe, a.binaries, a.zipfiles, a.datas, my_project_tree, ....) 

You must add the project folder to the dist folder. The problem is that you end up distributing the pyc of your project as well, but you still cannot find a better way. Very interested in a valid solution.

0
source

Source: https://habr.com/ru/post/899709/


All Articles