0

I'm trying to convert my .py file into a .exe.

The app works until it is in exe form, and I get the following errors, generated from http requests coming from the textmagic library:

  File "main.py", line 88, in <module>
  File "main.py", line 20, in send_generics
  File "textmagic/rest/models/messages.py", line 91, in create
  File "textmagic/rest/models/base.py", line 214, in create_instance
  File "textmagic/rest/models/base.py", line 156, in request
  File "textmagic/rest/models/base.py", line 121, in make_tm_request
  File "textmagic/rest/models/base.py", line 86, in make_request
  File "httplib2/__init__.py", line 1558, in request
  File "httplib2/__init__.py", line 1077, in __init__
  File "httplib2/__init__.py", line 172, in _build_ssl_context
FileNotFoundError: [Errno 2] No such file or directory

I've scavenged an identical issue with shotgun API on this forum and tweaked around patrick-hubert-adsk's response. This didn't work, but I may be doing something wrong here, particularly with the dst:

pyinstaller --add-data "`python3 -c 
'import httplib2; 
from httplib2 import certs;
import os; 
cacerts = certs.where(); 
print("%s:textmagic%s" % (cacerts, os.path.dirname(cacerts[len(httplib2.__path__[0]):])))'`" 
main.py

Any help is appreciated.

Daniel
  • 67
  • 1
  • 1
  • 7
  • are you sure the .exe has access to all of your packages? – Flow Jun 14 '22 at 00:56
  • Hi @Flow, I wouldn't be surprised if this is the issue considering the forum post I linked in the question. How could I ensure/validate that it has access to the packages, specifically to the certs that it doesn't seem able to find? – Daniel Jun 15 '22 at 03:04
  • when you use pyinstaller does it generate a file called dist? @Daniel if so that is where all your packages should be – Flow Jun 15 '22 at 05:00

1 Answers1

3

I would recommend specifying a few options on how to tell it to find & add -> bundling the files needed in your exe.

Before looking at the options below, basically you can tell it the path via command line while running it

  1. On adding the files to your exe.; and bundling the files correctly into your exe.

  2. This is important because it unpacks them during install, and using the os.path.join os.path.join etc.. as show in the samples below so it puts it in correct directory for the corresponding OS.


1 Adding/Locating files:

To ensure the files and their paths are found correctly, please list arguments in command prompt to be used/found by pyinstaller; --add-data "yourPath_to_file:yourPath_in_executable in directory"

// fore .g. here favicon.png is located in directory of socketserver.py 
// which is later copied to the root directory of the executable (the `.` signifies root directory)
pyinstaller --add-data "favicon.png;." --onefile yourPythonProgam.py

2 Bundling files into your executables

I am assuming you know how to edit the spec file, if not here is a ref.

Also if its a --onedir distribution, just pass a list of files (in TOC format) to the COLLECT, and they will show up in the distribution directory tree. The name in the (name, path, 'DATA') tuple can be a relative path name. Then, at runtime, you can use code like this to find the file:

os.path.join(os.path.dirname(sys.executable), relativename))

And in a --onefile distribution, your/data files are bundled in the executable, and when its run its then extracted at runtime into the work directory, that directory is best found by os.environ['_MEIPASS2']. So, you can access those bundled files through ref: MEIPASS:

os.path.join(os.environ["_MEIPASS2"], relativename))
from os import path
bundle_dir = path.abspath(path.dirname(__file__))
path_to_dat = path.join(bundle_dir, 'other-file.dat')

What is MEIPASS/2


  • Ref for your [Spec file][4]

PyInstaller does is to build a spec (specification) file myscript.spec. That file is stored in the --specpath directory, by default the current directory

  • Runtime to find your bundled exe file

Pyinstaller MEIPASS A

  • Adding files info:

Ref to Pyinstaller adding files

Transformer
  • 6,963
  • 2
  • 26
  • 52