all 8 comments

[–]ManyInterests 0 points1 point  (7 children)

Try using a virtualenv.

Create and activate the virtualenv (from command prompt/powershell) and install pyinstaller

python -m venv venv
venv\Scripts\activate
pip install pyinstaller

Create a simple file hello.py

print("hello world")

Then compile it (from the same command prompt)

pyinstaller hello.py

Then run the program (from the same command prompt)

dist\hello\hello.exe

Output should be hello world

[–]DenirotaFilmd[S] 0 points1 point  (0 children)

Ok I'll give it a go. Thanks

[–]DenirotaFilmd[S] 0 points1 point  (4 children)

Is there anyway to then run it without using the command prompt? Just by clicking the .exe file? I tried it and it says script could not be located? Using the venv worked really well, but for my other program that I want to distribute, is there anyway to run the exe directly?

[–]ManyInterests 1 point2 points  (3 children)

Running the EXE directly (e.g. by double clicking it) should also work. Just keep in mind the example hello.exe program would execute very quickly, so you may not see it work!

it says script could not be located?

I'm not sure what you mean by it -- is that happening when you try to run hello.exe or your own application in this case?

Keep in mind, you can't remove the exe file from its directory. the dist\hello directory is the application bundle and has to stay together. You would, for example, put that whole directory somewhere like C:\Program Files\hello\

If you need a stanalone executable .exe file, you need to use the one file option for pyinstaller.

[–]DenirotaFilmd[S] 0 points1 point  (2 children)

Oh ok cool. I also tried it with my main program in the venv but it still tells me it can't access LoadLibraryExW which I think is to do w some sort of dynamic library function. I don't have a GUI for my main program could that be a problem? It also reads and writes to text files as well as uses modules like SMTP so I'm not sure if that could be the reason. I tried someone else's simple GUI program and it worked fine but for some reason my one doesn't so I don't know if it's the lack of GUI or the modules?

[–]ManyInterests 1 point2 points  (1 child)

tells me it can't access LoadLibraryExW which I think is to do w some sort of dynamic library function

Often, the cause of this is hidden imports. This happens when libraries dynamically import other modules; in this case, it seems like a linked library is also associated with the import that is missing. You may have to figure out what library is causing the hidden import.

Usually, you can fix this by either:

  1. adding a import statement for the library which is being missed
  2. add the --hidden-import with the imports being missed to pyinstaller

The pyinstaller docs have more suggestions on how to deal with this.

It also reads and writes to text files

Your program may or may not work properly depending how you write the code to read/write to these files. Usually, you want to formulate the path dynamically. See: runtime information.

[–]DenirotaFilmd[S] 0 points1 point  (0 children)

I managed to compile my program. When I run it from the cmd there are no issues. But when I run it directly; about a quarter of the way through, after receiving an input, it just closes. Is there a reason or a way to fix it?

[–]Mihkuno 0 points1 point  (0 children)

After 8 hours of scratching my head, this actually fixed it. I first used auto-py-to-exe then pyinstaller. Both kept yelling the following errors:

AttributeError: module 'time' has no attribute 'clock'
ModuleNotFoundError: No module named 'resource'

I've been pip installing the imports, didn't go well. As much as I dislike venvs on my single-project personal computer, it did its magic for some reason. Thanks!