Pyinstaller ошибка при компиляции файла

Есть проект на python, где используется библиотека pyproj. Проблема в том, что конечный файл нужно скомпилировать в файл .exe, но сделать это не получается по непонятным причинам. Вот пример.

from PyQt5.QtWidgets import (QPushButton, QLabel, QVBoxLayout, QApplication, QWidget)
import sys
import pyproj

class Main(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(250, 100)
        self.setWindowTitle('Compile')
        self.btn = QPushButton('Press!')
        self.lbl = QLabel()
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.lbl)
        self.layout.addWidget(self.btn)
        self.setLayout(self.layout)

        self.btn.clicked.connect(self.on_btn)

    def on_btn(self):
        inProj = pyproj.Proj(init='epsg:4326')
        outProj = pyproj.Proj("+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs")
        x1,y1 = 45.387456, 30.524796
        lon,lat = pyproj.transform(inProj,outProj,x1,y1)

        self.lbl.setText(str(lon) + ' ' + str(lat))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Main()
    win.show()
    sys.exit(app.exec_())

В таком виде файл компилируется. Но при попытке открыть его двойным щелчком на секунду появляется консоль и исчезает, больше ничего не происходит.
Если убрать импорт pyproj, а также его объекты и методы из скрипта, то все компилируется и работает нормально. То есть проблема в библиотеке. Подскажите, пожалуйста, как можно решить эту проблему.

На всякий случай отмечу, что как скрипт python файл запускается без проблем.

Python 3.6, Pyinstaller 3.4, Windows 10 (64-разрядная)

Pyinstaller & shutil [function copy] problem !

hi I’m trying to convert a .py code into .exe
actually my code is similar to that :

 dr  = os.environ['WINDIR']
    current = os.path.abspath(__file__)
    shutil.copy(current, dr)

as we can see the code copy the current fine to windows file (this is just an example ) ; the code actually works fine but if we compile it into exe and run it we will see .

Traceback (most recent call last):
  File "<string>", line 13, in <module>
  File "C:\Documents and Settings\john\Desktop\pyinstaller\pyinstaller-2.0\test
\build\pyi.win32\test\out00-PYZ.pyz\shutil", line 119, in copy
  File "C:\Documents and Settings\john\Desktop\pyinstaller\pyinstaller-2.0\test
\build\pyi.win32\test\out00-PYZ.pyz\shutil", line 82, in copyfile
IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\john
\\Desktop\\pyinstaller\\pyinstaller-2.0\\test\\dist\\test.py'

the error is from function copy because it doesn’t find test.py in \test\dist\test.py .

any solutions ?

i tried :

 dr  = os.environ['WINDIR']
    current = os.path.abspath(__file__)
    newfile = current.replace('py','exe')
    shutil.copy(current, dr)

but it still say

IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\john
\\Desktop\\exeinstaller\\exeinstaller-2.0\\test\\dist\\test.exe'

The information above covers most normal uses of PyInstaller.
However, the variations of Python and third-party libraries are
endless and unpredictable.
It may happen that when you attempt to bundle your app either
PyInstaller itself, or your bundled app, terminates with a Python traceback.
Then please consider the following actions in sequence, before
asking for technical help.

Recipes and Examples for Specific Problems¶

The PyInstaller FAQ page has work-arounds for some common problems.
Code examples for some advanced uses and some common
problems are available on our PyInstaller Recipes page.
Some of the recipes there include:

  • A more sophisticated way of collecting data files
    than the one shown above (Adding Files to the Bundle).

  • Bundling a typical Django app.

  • A use of a run-time hook to set the PyQt5 API level.

  • A workaround for a multiprocessing constraint under Windows.

and others.
Many of these Recipes were contributed by users.
Please feel free to contribute more recipes!

Finding out What Went Wrong¶

Build-time Messages¶

When the Analysis step runs, it produces error and warning messages.
These display after the command line if the --log-level option allows it.
Analysis also puts messages in a warnings file
named build/name/warn-name.txt in the
work-path= directory.

Analysis creates a message when it detects an import
and the module it names cannot be found.
A message may also be produced when a class or function is declared in
a package (an __init__.py module), and the import specifies
package.name. In this case, the analysis can’t tell if name is supposed to
refer to a submodule or package.

The “module not found” messages are not classed as errors because
typically there are many of them.
For example, many standard modules
conditionally import modules for different platforms that may or may
not be present.

All “module not found” messages are written to the
build/name/warn-name.txt file.
They are not displayed to standard output because there are many of them.
Examine the warning file; often there will be dozens of modules not found,
but their absence has no effect.

When you run the bundled app and it terminates with an ImportError,
that is the time to examine the warning file.
Then see Helping PyInstaller Find Modules below for how to proceed.

Build-Time Dependency Graph¶

On each run PyInstaller writes a cross-referencing file about dependencies
into the build folder:
build/name/xref-name.html in the
work-path= directory is an HTML file that lists the full
contents of the import graph, showing which modules are imported
by which ones.
You can open it in any web browser.
Find a module name, then keep clicking the “imported by” links
until you find the top-level import that causes that module to be included.

If you specify --log-level=DEBUG to the pyinstaller command,
PyInstaller additionally generates a GraphViz input file representing the
dependency graph.
The file is build/name/graph-name.dot in the
work-path= directory.
You can process it with any GraphViz command, e.g. dot,
to produce
a graphical display of the import dependencies.

These files are very large because even the simplest “hello world”
Python program ends up including a large number of standard modules.
For this reason the graph file is not very useful in this release.

Build-Time Python Errors¶

PyInstaller sometimes terminates by raising a Python exception.
In most cases the reason is clear from the exception message,
for example “Your system is not supported”, or “Pyinstaller
requires at least Python 3.7”.
Others clearly indicate a bug that should be reported.

One of these errors can be puzzling, however:
IOError("Python library not found!")
PyInstaller needs to bundle the Python library, which is the
main part of the Python interpreter, linked as a dynamic load library.
The name and location of this file varies depending on the platform in use.
Some Python installations do not include a dynamic Python library
by default (a static-linked one may be present but cannot be used).
You may need to install a development package of some kind.
Or, the library may exist but is not in a folder where PyInstaller
is searching.

The places where PyInstaller looks for the python library are
different in different operating systems, but /lib and /usr/lib
are checked in most systems.
If you cannot put the python library there,
try setting the correct path in the environment variable
LD_LIBRARY_PATH in GNU/Linux or
DYLD_LIBRARY_PATH in macOS.

Getting Debug Messages¶

The --debug=all option (and its choices) provides a significant amount of diagnostic information.
This can be useful during development of a complex package,
or when your app doesn’t seem to be starting,
or just to learn how the runtime works.

Normally the debug progress messages go to standard output.
If the --windowed option is used when bundling a Windows app,
they are sent to any attached debugger. If you are not using a debugger
(or don’t have one), the DebugView the free (beer) tool can be used to
display such messages. It has to be started before running the bundled
application.

For a --windowed macOS app they are not displayed.

Consider bundling without --debug for your production version.
Debugging messages require system calls and have an impact on performance.

Getting Python’s Verbose Imports¶

You can build the app with the --debug=imports option
(see Getting Debug Messages above),
which will pass the -v (verbose imports) flag
to the embedded Python interpreter.
This can be extremely useful.
It can be informative even with apps that are apparently working,
to make sure that they are getting all imports from the bundle,
and not leaking out to the local installed Python.

Python verbose and warning messages always go to standard output
and are not visible when the --windowed option is used.
Remember to not use this for your production version.

Figuring Out Why Your GUI Application Won’t Start¶

If you are using the --windowed option,
your bundled application may fail to start with an error message like
Failed to execute script my_gui.
In this case, you will want to get more verbose output to find out
what is going on.

  • For macOS, you can run your application on the command line,
    i.e. ./dist/my_gui
    in Terminal instead of clicking on my_gui.app.

  • For Windows, you will need to re-bundle your application without the
    --windowed option.
    Then you can run the resulting executable from the command line,
    i.e. my_gui.exe.

  • For Unix and GNU/Linux there in no --windowed option.
    Anyway, if a your GUI application fails,
    you can run your application on the command line,
    i.e. ./dist/my_gui.

This should give you the relevant error that is preventing your
application from initializing, and you can then move on to other
debugging steps.

Operation not permitted error¶

If you use the –onefile and it fails to run you program with error like:

./hello: error while loading shared libraries: libz.so.1:
failed to map segment from shared object: Operation not permitted

This can be caused by wrong permissions for the /tmp directory
(e.g. the filesystem is mounted with noexec flags).

A simple way to solve this issue is to set,
in the environment variable TMPDIR,
a path to a directory in a filesystem mounted without noexec flags, e.g.:

Helping PyInstaller Find Modules¶

Extending the Path¶

If Analysis recognizes that a module is needed, but cannot find that module,
it is often because the script is manipulating sys.path.
The easiest thing to do in this case is to use the --paths option
to list all the other places that the script might be searching for imports:

pyi-makespec --paths=/path/to/thisdir \
             --paths=/path/to/otherdir myscript.py

These paths will be noted in the spec file in the pathex argument.
They will be added to the current sys.path during analysis.

Extending a Package’s __path__

Python allows a script to extend the search path used for imports
through the __path__ mechanism.
Normally, the __path__ of an imported module has only one entry,
the directory in which the __init__.py was found.
But __init__.py is free to extend its __path__ to include other directories.
For example, the win32com.shell.shell module actually resolves to
win32com/win32comext/shell/shell.pyd.
This is because win32com/__init__.py appends ../win32comext to its __path__.

Because the __init__.py of an imported module
is not actually executed during analysis,
changes it makes to __path__ are not seen by PyInstaller.
We fix the problem with the same hook mechanism we use for hidden imports,
with some additional logic; see Understanding PyInstaller Hooks below.

Note that manipulations of __path__ hooked in this way apply only
to the Analysis.
At runtime all imports are intercepted and satisfied from within the
bundle. win32com.shell is resolved the same
way as win32com.anythingelse, and win32com.__path__
knows nothing of ../win32comext.

Once in a while, that’s not enough.

Changing Runtime Behavior¶

More bizarre situations can be accommodated with runtime hooks.
These are small scripts that manipulate the environment before your main script runs,
effectively providing additional top-level code to your script.

There are two ways of providing runtime hooks.
You can name them with the option --runtime-hook=path-to-script.

Second, some runtime hooks are provided.
At the end of an analysis,
the names in the module list produced by the Analysis phase are looked up in
loader/rthooks.dat in the PyInstaller install folder.
This text file is the string representation of a
Python dictionary. The key is the module name, and the value is a list
of hook-script pathnames.
If there is a match, those scripts are included in the bundled app
and will be called before your main script starts.

Hooks you name with the option are executed
in the order given, and before any installed runtime hooks.
If you specify --runtime-hook=file1.py --runtime-hook=file2.py then the execution order at runtime will be:

  1. Code of file1.py.

  2. Code of file2.py.

  3. Any hook specified for an included module that is found
    in rthooks/rthooks.dat.

  4. Your main script.

Hooks called in this way, while they need to be careful of what they import,
are free to do almost anything.
One reason to write a run-time hook is to
override some functions or variables from some modules.
A good example of this is the Django runtime
hook (see loader/rthooks/pyi_rth_django.py in the
PyInstaller folder).
Django imports some modules dynamically and it is looking
for some .py files.
However .py files are not available in the one-file bundle.
We need to override the function
django.core.management.find_commands
in a way that will just return a list of values.
The runtime hook does this as follows:

import django.core.management
def _find_commands(_):
    return """cleanup shell runfcgi runserver""".split()
django.core.management.find_commands = _find_commands

Getting the Latest Version¶

If you have some reason to think you have found a bug in PyInstaller
you can try downloading the latest development version.
This version might have fixes or features that are not yet at PyPI.
You can download the latest stable version and the latest development
version from the PyInstaller Downloads page.

You can also install the latest version of PyInstaller directly
using pip:

pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip

Asking for Help¶

When none of the above suggestions help,
do ask for assistance on the PyInstaller Email List.

Then, if you think it likely that you see a bug in PyInstaller,
refer to the How to Report Bugs page.

Олег

@dotokbit

В начале пути

Пробую компилировать программу, написанную в python в exe файл с помощью pyinstaller. Выдает простыню ошибок
На выходе пустая папка dist и build с файлами, но exe нет
601a5b3462c6c830589369.png:


  • Вопрос задан

  • 486 просмотров


Комментировать

Пригласить эксперта


Ответы на вопрос 2

SoreMix

Версия 3.3.1 вышла за сто лет до python 3.9

Олег

@dotokbit Автор вопроса

В начале пути

Все огромное спасибо! Вопрос решен!
Решение: нужно было предварительно прописать pip install wheel


Комментировать


Похожие вопросы


  • Показать ещё
    Загружается…

21 сент. 2023, в 19:28

10000 руб./за проект

21 сент. 2023, в 19:06

11111 руб./за проект

21 сент. 2023, в 19:00

6000000 руб./за проект

Минуточку внимания

Mediapipe library requires certain files to run (these are mainly TensorFlow files with .tflite extension.)
Since these are loaded dynamically, PyInstaller doesn’t know about these and during the compilation phase you need to copy them over to the dist directory.

This can be achieved via datas property in PyInstaller spec file.
You can read more about it in the PyInstaller docs

Here is my spec file. See under datas property and ensure that path is correct since directories under venv are OS specific.

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['main.py'],
             pathex=['.'],
             binaries=[],
             datas=[('venv\\Lib\\site-packages\\mediapipe\\modules', 'mediapipe\\modules'),],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='main',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='main')

Понравилась статья? Поделить с друзьями:
  • Pycharm ошибка при импорте
  • Pygame init ошибка
  • Ps5074 fanuc ошибка
  • Pygame error font not initialized ошибка
  • Pyflakes e ошибка