7

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 seems to run fine (at least on executable version...). Now everything is working fine, in one-file mode, with use of accepted answer's function to access resource files!


Edit 2: as shown in accepted answer's comments, problem is coming from path resolution in my script; I try to use __file__ to get the location of the script, so that I can access to its resource files. This does not work once packaged, as __file__ will return filename from Python.dll to the script, so quite always no path and just a file name. So I have to find another trick to make access to resource files; a work-around for the moment is to move current directory to the executable path.

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

I'll update this question with the way I resolved this path resolution question.


I've got problems with pyinstaller, and as it's the first time I'm using it, it's sure that I've done something wrong.

So, here's the problem: pyisntaller runs smoothly on a script I wrote, and generates some stuff in dist folder. Ok, so now I want to execute it to see if all went well, and here's 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 was missing, so I added it (and some other resource files) in the p_tool.spec file, but this is not better.

Python version: 2.6.6, under WinXP. I'm using pyinstaller as I will need it to package files for a Solaris workstation.

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


Edit3: details about logging removed, as not really related to the problem.

2

3 Answers 3

10
+50

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

As to actually accessing it, os.path.split(__file__) looks almost correct, but I'm not sure it works properly under pyinstaller - the proper way of packing files is to add them to the .spec file, pyinstaller will then load them at compile time and unpack them to $_MEIPASS2/ at run time. To get the _MEIPASS2 dir in packed-mode and use the local directory in unpacked (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"
3
  • This answer is full of good advice. So, first, printing the path I'm looking for is showing that __file__ is resolved as the script, relative to Python.dll, which means that in my case, os.path.split(...)[0] is always empty. Said otherwise, it's as if my script would be local to any location where it's called... So now I have to find out another way to access my resource files. :-/
    – Joël
    Commented Oct 25, 2011 at 10:54
  • To complete previous comment: if I move to executable folder before executing it, path resolution is fine, and now tool starts correctly! So, now I have to understand how to define the path resolution inside my Python code to make it work, and it's another topic. About resource_path function, it'll surely be useful when I'll try one-file packaging. So, your answer is worth the bounty! Thanks for your help :-)
    – Joël
    Commented Oct 25, 2011 at 11:05
  • MEIPASS or MEIPASS2? How do I know?
    – dranxo
    Commented Nov 6, 2013 at 3:37
0

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

1
  • I updated the question, but I doubt it's really a problem from this file, as Python version runs without problem. So I think that's more a problem with access to the file.
    – Joël
    Commented Oct 24, 2011 at 16:13
0

I had a similar problem but couldn't find a elegant fix so far. The 'hack' I use that got me trough, say my project is located 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 the Analysis object, basically I remove all of my projects files from the PYZ so no import is passed there and the logger's relative paths won't be computed from there. After this, create a Tree object from the project.

my_project_tree = Tree('~/project')

Then add this Tree to the list of TOC that is passed to COLLECT, so :

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

You should have your project folder added to the dist folder. The problem is that you'll end up distributing the pyc's of your project too, but couldn't find a better way so far. Very interested in the valid solution.

6
  • Mmh ok, seems tricky but not beyond my reach. Question though: will the pyc files be executed with the embedded Python executable? I mean, will this run on any computer with or without Python installed?
    – Joël
    Commented Oct 24, 2011 at 16:33
  • Yes they should be, as long as it's the same os. I've only tested it on debian squeeze however so can't guarantee.
    – Bogdan
    Commented Oct 24, 2011 at 16:54
  • I added some more details about logging configuration. Could you please have a look? Maybe the way I configured my resource files is leading to problems with PyInstaller.
    – Joël
    Commented Oct 25, 2011 at 7:29
  • Second comment, about your code: are you using the excluded_sources as follows: pyz = PYZ(a.pure - excluded_sources)? If yes, then maybe the not is too many in for x in a.pure if *not* x[0].startswith(.... I may have not understood it, as it's still not working; but having pyc files is still better than nothing, if packaged for any Python version.
    – Joël
    Commented Oct 25, 2011 at 7:31
  • Here're some news. Problem was coming from the fact that __file__ was not returning the expected value. So, I end up using sys.argv[0] to determine location of the called executable. I hope there's no side-effect about this, as it's all I could imagine to work-around this issue. Thanks for your help anyway!
    – Joël
    Commented Oct 25, 2011 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.