Python Forum
Can a module be executed even if the computer running it does not install it? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Can a module be executed even if the computer running it does not install it? (/thread-24316.html)



Can a module be executed even if the computer running it does not install it? - CFYB - Feb-08-2020

Hello, I'm French, so sorry for the spelling mistakes.
I ask myself a question about the modules:
if for example I make a game with pygame and I give the game to a friend, will he be able to make it work without having installed it in advance ? Huh

Thank you in advance for all the answers you can give me . Smile


RE: Can a module be executed even if the computer running it does not install it? - larryb - Feb-08-2020

This may help: https://stackoverflow.com/questions/14368246/using-pygame-without-installing


RE: Can a module be executed even if the computer running it does not install it? - metulburr - Feb-08-2020

The easiest way to do that is to create an exe file with PyInstaller which will bundle Python and Pygame into it so the end user does not need to do anything other than run it.

You can just copy the Pygame libs over, but that is prone to error. If you dont know what your doing it can just simply not work with odd errors. I would not suggest this method.


RE: Can a module be executed even if the computer running it does not install it? - snippsat - Feb-08-2020

To do a test using @metulburr pong example here.
Build in a virtual environment,so all install of Pygame and Pyinstaller is new.
Full run:
# Make environment
E:\div_code
λ python -m venv pygame_env

# Cd in
E:\div_code
λ cd pygame_env\

# Activate environment
E:\div_code\pygame_env
λ E:\div_code\pygame_env\Scripts\activate

# Install
(pygame_env) E:\div_code\pygame_env
λ pip install pygame pyinstaller
Collecting pygame .....  
Successfully installed altgraph-0.17 future-0.18.2 pefile-2019.4.18 pygame-1.9.6 pyinstaller-3.6 pywin32-ctypes-0.2.0

# Build with Pyinstaller
(pygame_env) E:\div_code\pygame_env
λ pyinstaller --onefile pong.py
148 INFO: Python: 3.7.3
149 INFO: Platform: Windows-10-10.0.18362-SP0
.....
14637 INFO: Appending archive to EXE E:\div_code\pygame_env\dist\pong.exe
14679 INFO: Building EXE from EXE-00.toc completed successfully.

# Cd to dist
(pygame_env) E:\div_code\pygame_env
λ cd dist

# List files use dir for cmd
(pygame_env) E:\div_code\pygame_env\dist
λ ls
pong.exe*

# Test run and it work
(pygame_env) E:\div_code\pygame_env\dist
λ pong
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
side wall hit, time to reset ball and give points
So it works,for more advance games with a addition data file like image/sound ect... may need to add this in build process.
Pyinstaller has good documentation about this.
Example on how that would look:
pyinstaller --onefile --icon=sound.ico --add-data shoot.png;. pong.py
For more addition can also use spec file that Pyinstaller create on every run.


RE: Can a module be executed even if the computer running it does not install it? - CFYB - Feb-08-2020

Yes but PyInstaller is a program so I have to install it so I always have the same problem, there would be no way to execute a command in a console from a python program (to execute the installation of the desired module)?
  I suppose I have to import the os module, but I think it is present in the language?


RE: Can a module be executed even if the computer running it does not install it? - snippsat - Feb-08-2020

(Feb-08-2020, 01:35 PM)CFYB Wrote: Yes but PyInstaller is a program so I have to install it so I always have the same problem
Yes you have to do pip install pyinstaller,and build as i have shown over.
You said you want to share with your friend so if give him pong.exe(you can test run it) just double click in on pong.exe file and game start no installation for him.
(Feb-08-2020, 01:35 PM)CFYB Wrote: I suppose I have to import the os module, but I think it is present in the language?
No,i have no idea what you talking about here?