Tyler Morgan
Tyler Morgan

Reputation: 43

Why does my python (exe file) not work correctly when opened as a exe file?

I have a python file that is simple and opens up webpages from a text file. It is supposed to work and even works in visual studios. I decided to turn it into a exe file using pyinstaller. It outputted a exe file just fine and I opened it but it did nothing and closed right after I opened it.

I used Python 3 and downloaded the pyinstaller inside of Anaconda. Thank you

My code:

import webbrowser
print('Welcome to my project')
webfile = open('webfile.txt', 'r')
for lines in webfile:
    webbrowser.open(lines)

k = input('Press Enter to Exit')

Upvotes: 3

Views: 1449

Answers (1)

Vikramaditya Gaonkar
Vikramaditya Gaonkar

Reputation: 717

Most of the times, when it comes to reading a file via the pyinstaller bundeled executable, you do not have the right path to the file. Here are some ways you can get around it:

  1. Enter the full path of the file. Note that this would not work when you want to use the executable on another machine. For example use webfile = open('<full_path_to webfile.txt>', 'r')
  2. As your local paths are not the same when you have bundeled your application, it seems most likely that you will have to use an if condition to set the path of the expected file. There are many answers (here, here) on SO which will explain this in further detail.

PS: It is not a good practice to read the file without a context manager. This will explain why.

Upvotes: 1

Related Questions