2

I have been trying to bundle a json file in a python executable created with pyinstaller. After a lot of researching, the solution I found involved making use of the _MEIPASS folder; however, VSCode claims that the sys package has no _MEIPASS member.

The relevant part of my code goes like this:

branches_path = 'bank_branches/bank_branches.json'
if hasattr(sys, "_MEIPASS"):
 branches_path = os.path.join(sys._MEIPASS, branches_path)

The code works on the terminal version, as well as on the standalone application, so this is taken care of; however, I'd like to know if there is a solution which works and has no errors associated. If it helps, I'm using Python 3.6.6

asked Jul 5, 2018 at 18:23

1 Answer 1

5

I ran into a similar issue when creating an executable using pyinstaller. I had to make two changes to my script in order to get a functional executable.

First, I created this function:

def resource_path(relative_path):
 try: 
 base_path = sys._MEIPASS
 except Exception:
 base_path = os.path.abspath(".")
 return os.path.join(base_path, relative_path)

My script has several classes in it, so I put this at the very end, by itself, so that all classes could reference it. I then replaced any function I had that used

os.getcwd()

- which was probably a bad idea in the first place - with

resource_path() 

and for the variable inside of resource_path() I used this function instead:

os.path.dirname(os.path.abspath(__file__))

This function returned what I wanted anyway; the location of THIS file/program that is running.

So, what previously was written like:

filePath = os.getcwd() + "\\my_file.csv"

Now reads as:

filePath = resource_path(os.path.dirname(os.path.abspath(__file__))) + "\\my_file.csv"

Once this was in place, my program compiled correctly and executed as expected, hopefully it can help you as well.

answered Aug 23, 2018 at 19:24
Sign up to request clarification or add additional context in comments.

4 Comments

I'll try it out this weekend, but in the meantime I appreciate your help!
I have the same problem but at an executable file made with cx_Freeze. 'AttributeError: module 'sys' has no attribute '_MEIPASS'' . So what could I do in this case ? Thank
@chaviaras Michalis - did this method not work for you in cx_Freeze? because it should.
@NL23codes I had a very big project and I couldn't replace with the above every call. I had the same _MEIPASS problem but at the end I solved it differently. I notice that there was problem with pyocr library and tkinter. I am suspecting either the cx_Freeze or the setup.py I wrote because these two libraries were different in the Python Path and at the folder I made the setup.py. So at the end I can't say to you if this method works for the cx_Freeze because I didn't do this method.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.