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.

3
  • are you sure the .exe has access to all of your packages? Commented Jun 14, 2022 at 0: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? Commented Jun 15, 2022 at 3:04
  • when you use pyinstaller does it generate a file called dist? @Daniel if so that is where all your packages should be Commented Jun 15, 2022 at 5:00

1 Answer 1

3
+25

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

Sign up to request clarification or add additional context in comments.

2 Comments

Never got a chance to thank you for this amazing response. Kudos, sir.
You are welcome Daniel :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.