pyinstaller не находит путь к файлу внутри программы - PullRequest
0 голосов
/ 12 мая 2018

Я создал инструмент тестирования, чтобы проверить, есть ли какие-либо модули, которые не работают с pyinstaller, поэтому я могу их отработать перед использованием pyinstaller в моей основной программе.

Когда я пытаюсь взаимодействовать с путями к файлам в моем скрипте, создается впечатление, что программа, созданная pyinstaller, не может найти пути, которые я пытался записать в скрипт, например, "Z: \ mkb \ crew \ mark_conrad \ pictures \ psd_tool_test_files \ test.psd». Я решил использовать просто os.path.exists () для устранения этой загадки, но безуспешно. Когда я запускаю свою программу отладки из консоли Python, она работает просто отлично, так что здесь не так?

Как я создал exe: pyinstaller "Z: \ mkb \ programing \ python \ util \ pyinstaller_library_tester.py"

Версия Python: 2.7.15 Версия PyInstaller: 3.3.1

Консульский вывод:

Testing: Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd
>>> This path does not exsist.
Path Results: False

Testing: Z:\\mkb\\crew\\mark_conrad\\pictures\\psd_tool_test_files\\test.psd
>>> This path does not exsist.
Path Results: False

Testing: Z:/mkb/crew/mark_conrad/pictures/psd_tool_test_files/test.psd
>>> This path does not exsist.
Path Results: False

Testing: Z://mkb//crew//mark_conrad//pictures//psd_tool_test_files//test.psd
>>> This path does not exsist.
Path Results: False

Код программы отладки:

def checkingPaths(path,btn):
    import os

    if os.path.exists(path):
        print '>>> Found a working path use this for your formats for paths'
        print 'Path Results:',os.path.exists(path)
        btn.configure(bg='#00cc30')
    else:
        print '>>> This path does not exsist.'
        print 'Path Results:',os.path.exists(path)
        btn.configure(bg='#ff0000')

def osTest(btn):

    print r'Testing: Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd'
    checkingPaths("Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd",btn)

    print r'Testing: Z:\\mkb\\crew\\mark_conrad\\pictures\\psd_tool_test_files\\test.psd'
    checkingPaths("Z:\\mkb\\crew\\mark_conrad\\pictures\\psd_tool_test_files\\test.psd",btn)

    print r'Testing: Z:/mkb/crew/mark_conrad/pictures/psd_tool_test_files/test.psd'
    checkingPaths("Z:/mkb/crew/mark_conrad/pictures/psd_tool_test_files/test.psd",btn)

    print r'Testing: Z://mkb//crew//mark_conrad//pictures//psd_tool_test_files//test.psd'
    checkingPaths("Z://mkb//crew//mark_conrad//pictures//psd_tool_test_files//test.psd",btn)

def tkinterTest():
    import Tkinter as tk

    root = tk.Tk()

    osBtn = tk.Button(root,text='os Test',command =lambda: osTest(osBtn))

    osBtn.pack(padx=10,pady=2,fill='x')

    root.mainloop()

tkinterTest()

1 Ответ

0 голосов
/ 12 мая 2018

Создает новый путь, который вы должны использовать при «компиляции» в sys._MEIPASS. Обычно я делаю функцию, которая разрешает относительный путь к ресурсу в зависимости от того, работает ли он в Python или когда он «скомпилирован», например:

def get_correct_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

Также убедитесь, что вы правильно включили файлы в файл спецификации.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...