programing tip

Python 스크립트를 독립 실행 형으로 실행하여 종속성없이 실행하는 방법은 무엇입니까?

itbloger 2020. 10. 3. 10:05
반응형

Python 스크립트를 독립 실행 형으로 실행하여 종속성없이 실행하는 방법은 무엇입니까? [닫은]


저는 Python 애플리케이션을 구축 중이며 클라이언트가 Python과 모듈을 설치하도록 강요하고 싶지 않습니다. 또한 내 애플리케이션을 비공개 소스로 만들고 싶습니다.

그렇다면 Python 스크립트를 독립 실행 형 실행 파일로 컴파일하는 방법이 있습니까?


당신은 사용할 수 있습니다 py2exe에를 이미 대답 사용으로 사이 썬 키 변환 할 .py파일을 .pyc같은 C 컴파일 된 파일, .dll윈도우와 .so일반보다 되돌릴 더 힘들어, 리눅스에서 .pyo.pyc파일 (이득 성능도하고!)


PyInstaller사용 하여 Python 프로그램을 독립 실행 형 실행 파일로 패키징 할 수 있습니다 . Windows, Linux 및 Mac에서 작동합니다.

PyInstaller 빠른 시작

PyPI에서 PyInstaller를 설치합니다.

pip install pyinstaller

프로그램의 디렉토리로 이동하여 다음을 실행하십시오.

pyinstaller yourprogram.py

그러면라는 하위 디렉토리에 번들이 생성됩니다 dist.

자세한 안내는 매뉴얼을 참조하십시오 .


Nuitka 를 조사하는 것이 좋습니다 . 파이썬 소스 코드를 받아 C ++ API 호출로 변환합니다. 그런 다음 실행 가능한 바이너리 (Linux의 ELF)로 컴파일됩니다. 몇 년 동안 사용되어 왔으며 다양한 Python 버전을 지원합니다.

사용하면 성능이 향상 될 수도 있습니다. 추천합니다.


세 번째 옵션 cx_Freeze은 크로스 플랫폼입니다.


Python 2.7을 사용하여 Windows에서 독립 실행 형 파일을 만드는 방법에 대한 유용한 정보를 컴파일하고 싶습니다.

py2exe를 사용 했는데 작동하지만 몇 가지 문제가 있습니다.

이 마지막 이유 때문에 PyInstaller http://www.pyinstaller.org/를 사용해 보았습니다 .

제 생각에는 다음과 같은 이유로 훨씬 좋습니다.

  • 사용하기가 더 쉽습니다.

예를 들어 다음 줄로 .bat 파일을 만드는 것이 좋습니다 (pyinstaller.exe는 Windows 경로에 있어야 함).

pyinstaller.exe --onefile MyCode.py

그래서 적어도 파이썬 2.7의 경우 더 좋고 간단한 옵션은 PyInstaller라고 생각합니다.


예, Python 스크립트를 독립 실행 형 실행 파일로 컴파일 할 수 있습니다.

PyInstallerWindows , Linux , Mac OS X , FreeBSD , SolarisAIX에서 Python 프로그램을 독립 실행 형 실행 파일로 변환하는 데 사용할 수 있습니다 . 권장되는 변환기 중 하나입니다.

py2exe 는 Python 스크립트를 Windows 플랫폼에서만 실행 가능한 파일로 변환합니다.

Cython 은 Python 프로그래밍 언어와 확장 Cython 프로그래밍 언어 모두를위한 정적 컴파일러입니다.


당신은 py2exe를 좋아할 수 있습니다 . Linux에서 수행하는 정보도 찾을 수 있습니다.


py2exe를 사용하십시오 .... 아래 설정 파일을 사용하십시오.

 from distutils.core import setup
 import py2exe

 from distutils.filelist import findall
 import matplotlib

 setup(
       console=['PlotMemInfo.py'],

       options={
                'py2exe': {
                'packages' : ['matplotlib'],
            'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                 'libgobject-2.0-0.dll',
                 'libgdk_pixbuf-2.0-0.dll']
                          }
                },
       data_files = matplotlib.get_py2exe_datafiles()
     )

또한 python 2.3-2.7 과 같은 더 나은 이전 버전과의 호환성을 위해 pyinstaller권장 합니다.
대한 py2exe에 , 당신은 파이썬 2.6이 있어야


Python 3.2 스크립트의 경우 유일한 선택은 Cxfreeze입니다. 소스에서 빌드하지 않으면 작동하지 않습니다.

python 2.x의 경우 라이브러리도 출력하는 CxFreeze와 달리 Python 프로그램을 단일 실행 파일로 패키지 할 수 있으므로 pyinstaller를 제안합니다.


py2exe will make the exe file you want but you need to have the same version of MSVCR90.dll on the machine you're going to use your new exe. See http://www.py2exe.org/index.cgi/Tutorial for more info.


You can find the list of distribution utilities listed @ https://wiki.python.org/moin/DistributionUtilities.

I use bbfreeze and it has been working very well (yet to have python 3 support though).


Not exactly a packaging of the python code, but there is now also grumpy from google, which transpiles the code to Go. It doesn't support the python C api, so it may not work for all projects.


I like pyinstaller - especially the "windowed" variant:

pyinstaller --onefile --windowed myscript.py

It will create one single *.exe file in a dist/ folder.


Use Cython to convert to c, compile and link with gcc. Another could be, make the core functions in c (the ones you want to make hard to reverse), compile them and use python boost to import the compiled code ( plus you get a much faster code execution). then use any tool mentioned to distribute.


Using pyinstaller, I found a better method using shortcut to the .exe rather than making --onefile. Anyways there's probably some data files around and if you're running a site-based app then your program depends on html, js, css files too. No point in moving all these files somewhere.. instead what if we move the working path up.

Make a shortcut to the exe, move it at top and set the target and start-in paths as specified, to have relative paths going to dist\folder: Target: %windir%\system32\cmd.exe /c start dist\web_wrapper\web_wrapper.exe Start in: "%windir%\system32\cmd.exe /c start dist\web_wrapper\" Can rename shortcut to anything so renaming to "GTFS-Manager"
Now when I double-click the shortcut, it's as if I python-ran the file! I found this approach better than the --onefile one as:

  1. In onefile's case, there's a problem with a .dll missing for win7 OS which needs some prior installation etc. Yawn. With the usual build with multiple files, no such issues.
  2. All the files that my python script uses (it's deploying a tornado web server and needs a whole freakin' website worth of files to be there!) don't need to be moved anywhere: I simply create the shortcut at top.
  3. I can actually use this exact same folder in ubuntu (run python3 myfile.py) and windows (double-click the shortcut).
  4. I don't need to bother with the overly complicated hacking of .spec file to include data files etc.

Oh, remember to delete off the build folder after building, will save on size.


I'm told that PyRun, https://www.egenix.com/products/python/PyRun/, is also an option.

참고URL : https://stackoverflow.com/questions/5458048/how-to-make-a-python-script-standalone-executable-to-run-without-any-dependency

반응형