r/learnpython • • Jul 08 '20

How to share python exe with people to use the program without having to install python?

Hi All, I have written a my first very basic program for webscraping where it summaries the top 5 most read article and sends a email.

The user needs to enter their email ID and password to login using smtplib and that's all the input that is required.

I wanted to share this with my friends so they can use it at their end without having to install python.

Is it possible to do that? Basically i wanted to know once we write a program how do we create a user friendly front end app?

Many thanks in advance! Any inputs are highly appreciated!

399 Upvotes

71 comments sorted by

173

u/sme272 Jul 08 '20

Write your program and use pyinstaller to turn it into an exe when you're done. The exe will only work on systems that have the same operating system as the one it was created on, so if your friends use different OS's you'll need to create a virtual machine running that OS on your computer and create a separate exe for them.

50

u/knightwarrior911 Jul 08 '20

Many thanks! If I make it using windows 10 it will only work on my friends computer who has windows 10 right?

63

u/[deleted] Jul 08 '20

also you should convert the .py file extension to a .pyw file so that when you run your app no console pops up, if your app has a gui then you should convert it to a .pyw file if not then you're good. to convert it to a .pyw file you need to navigate to the directory where your file is stored using the command prompt in windows 10 and simple type in this

ren yourfilename.py to yourfilename.pyw

after that you can use pyinstaller to make it into an app to go to the directory in which your file is in you should use the cd command. when you open the command prompt it'll be normally in C:\users\Admin (if you installed your os in C: drive) if your file is in a folder named apps which is in the admin folder simply type in

cd apps

also if you're making a simple gui then all you need is tkinter if you wan't more features and want it to be good looking or better use pyQt

57

u/MacItaly if MacItaly: return helpful / else: return to_Google Jul 08 '20

You can compile a windowless .exe from the pyinstaller command line...

pyinstaller -w -F main.py

-w: makes a windowless .exe

-F: compiles your program and all files into a single .exe file

10

u/[deleted] Jul 08 '20

but when i type that in it gives me a error saying that scriptname is required

45

u/SweetSoursop Jul 08 '20 edited Jul 08 '20

replace main.py with the name of your .py file.

or just use tab to explore in the directory

1

u/[deleted] Jul 08 '20

so you don't need to use --onefile and you don't need to separately convert it into a no console file?? that's cool! thanks!

6

u/SweetSoursop Jul 08 '20

Just -F and -w before the script name.

respect the capitalization btw.

2

u/FewerPunishment Jul 08 '20

I recommend forcing yourself to read the documentation when learning how to use a new program or when you need to learn what inputs it takes. It's usually boring and tedious but by doing so you will realize that --onefile and -F mean the same thing (and you will learn many other things) https://pyinstaller.readthedocs.io/en/stable/usage.html#what-to-generate

2

u/[deleted] Jul 09 '20

thanks, will do

4

u/m3nt4l09 Jul 08 '20

You can also enable showing file extensions and change it like you're renaming the file.

2

u/wescotte Jul 08 '20

Is there a easy way to spawn console window (also ensuring stdout / stderr are directed to it) if you want? Say you create an app and want to allow a -noGUI argument or something.

My current solution was to just hide the console when the app was created if they were using the GUI. However, I don't like that that window hangs for a second or two before hiding.

if args.noGUI is False:
	if sys.platform.lower().startswith('win'):
		import ctypes
		whnd = ctypes.windll.kernel32.GetConsoleWindow()
		if whnd != 0:
			ctypes.windll.user32.ShowWindow(whnd, 0)

4

u/sme272 Jul 08 '20

yes, and I believe it also restricts it to the same architecture (32 or 64 bit OS) so you might need a virtual machine running a second copy of win 10.

2

u/Allanon001 Jul 08 '20

If you build on 32 bit version of Windows then it will work with both 32 bit and 64 bit because Windows 64 bit is backwards compatible.

4

u/[deleted] Jul 08 '20

As MacItaly has said, pyinstaller makes this trivial. The exe file will be abnormally large though (at least 6mb). Also if you make the exe with windows10 it will run on windows7 no problem. On older versions of Windows I have no idea

2

u/Allanon001 Jul 08 '20

If you build on a 32 bit version of Windows then it will be compatible with both 32 bit and 64 bit Windows because 64 bit Windows is backwards compatible. If you build on a 64 bit version of Windows then it will only work on the 64 bit version of Windows.

3

u/d3v3ndra Jul 08 '20

So, how we can develop app so it can run on both rhel and windows server. I start learning python as it is oop and works on both os.

13

u/sme272 Jul 08 '20

Python isn't really designed to distribute programs as compiled exe files, The best ways to deploy finished program are either install python on the target machine and use a requirements.txt file to install the dependencies or to make it a webapp that runs on a server and can be access by anyone that needs it via requests or a webpage.

If you really want to go the compiled exe route you'd have to make multiple exe's, one for each system configuration that it'll run on.

2

u/angry_mr_potato_head Jul 08 '20

Unless you use os specific calls, it should run on both OSes with little intervention for what its worth. But a self contained package that doesn't rely on python being on the machine already is a little more difficult.

2

u/[deleted] Jul 08 '20

Is there really no cross compiler?

6

u/sme272 Jul 08 '20

No, because python was never designed to be a compiled language.

4

u/[deleted] Jul 08 '20

That makes sense. I really don't know of anyone who compiles their python. And frankly I don't know many people using cross compilers outside of embedded development

2

u/LeSplooch Jul 09 '20

If you're curious about compiling Python, take a look at Nuitka.

1

u/ivosaurus Jul 09 '20

Pypy is compiling it! But Just In Time.

0

u/[deleted] Jul 08 '20

[deleted]

2

u/[deleted] Jul 08 '20

I'm not familiar with GNS

1

u/[deleted] Jul 08 '20

What if your project has dependencies or multiple files?

3

u/skellious Jul 08 '20

they can either be compiled into the exe (a one-file app) or be put in an accompanying folder.

exes are actually just a specialised sort of zip archive. if you open one in linux you can see the files inside it.

45

u/[deleted] Jul 08 '20

You could turn it into a very simple web app using Flask and host it on python anywhere. Users would be able to enter their email address, and the app would use your credentials to send the email. I would set up a new gmail account specifically for this.

13

u/knightwarrior911 Jul 08 '20

Thanks! I do not have much idea about this, kindly forgive my silly questions.

I wanted to know if hosting this web app can be done freely and does my pc need to be turned on to host it 24*7?

32

u/[deleted] Jul 08 '20

Yes, it can be hosted for free, and your computer won't need to be turned on. Check out https://www.pythonanywhere.com/
You can develop the code on your computer and then once it's working you can host it there

21

u/[deleted] Jul 08 '20

You can also use heroku, which I personally find easier to use than pythonanywhere.

1

u/knightwarrior911 Jul 08 '20

Their free account says that one app allowed with restricted access to the internet. Does that mean that the program cannot webscrape?

2

u/Biaswords_ Jul 08 '20

From what I can recall that is true. They have a set of white listed urls you have access to. I would say though, the investment is minimal and it’s such a handy thing to have. I think I have a custom plan and pay $11 a month. And use it daily

2

u/dragonlearnscoding Jul 08 '20

how would you use this daily

2

u/Biaswords_ Jul 08 '20

I host various flask scripts for coworkers and gadgets I made for myself

2

u/CotoCoutan Jul 09 '20

Check out their whitelist. Google PythonAnywhere whitelist, lot of websites on there already so hopefully your website is too.

1

u/OnlySeesLastSentence Jul 09 '20

Ah, looks like I still have to Django it, right?

I was hoping for like a repl.it or trinket.io site, but one that supports larger screensizes.

9

u/Edgar_A_Poe Jul 08 '20

Not OP but love the advice. Thanks a lot.

6

u/GrandBadass Jul 08 '20

Highly recommend this suggestion. Also because if you need to update the app then you can change it in the app and not have to regenerate and redistribute the exe file.

1

u/toonarmycapt Jul 12 '20

This isn't a bad idea at all. Learning to build a basic flask app is good, plus you can make an API that someone could use the terminal eg curl yourappurl [email protected].

I've used python anywhere: probableimmunity.pythonanywhere.com/immunity/ , without knowing much in terms of front end web stuff, but plan to figure out heroku at some point soon.

9

u/bootyofbamby Jul 08 '20

If you want some GUI(Graphical User Interface I guess), use pyqt5(you do not need to have a GUI to make an exe).Note that if you use anaconda do not pip install it,it is already installed.but after that It will still remain as py file.So you can use pyinstaller or py2exe to convert py files into exes.

8

u/Llamafu Jul 08 '20

Auto-py-to-exe

It's basically just a gui interface for pyinstaller, but it's way easier than dealing with spec files and messy imports. Best on python 3.5.

7

u/Nexius74 Jul 08 '20 edited Jul 08 '20

Just as a reminder pyinstaller don't work if you use some complex framework like Django or lib2to3. Your best bet (if you're using those kind of framework) is to bundle the python runtime and dependencies in one folder and run the script by using a batch script. Python official web site let you download an embedable python runtime.

Edit: The embedable runtime is only available for windows

4

u/you-cant-twerk Jul 08 '20

You're looking to "freezing your code". Pyinstaller will take care of that. Check out some youtube tutorials.

3

u/johnne86 Jul 08 '20

This is one of those things that make me want to learn JS/Electron or just C# if I want to target Desktop GUI apps. However, everything seems to be heading web based as well these days, might as well learn JS or learn Flask/Django if you stick to Python. I think I'd rather learn to make a Flask app than distribute an .exe, seems more beneficial in the long run.

This Remi project looks interesting for creating web based desktop Python GUIs: https://github.com/dddomodossola/remi/blob/master/README.md

3

u/[deleted] Jul 09 '20

Was reading through all the answers and was surprised to see no one mention anything about issues transferring and running compiled python scripts. I'm wondering if it was only me struggling. I've wrote a small script that parses an excel file and creates a new one. Then compiled it with autopy2exe. First issue I've had is that Gmail would flat out refuse to send the email with the attachment. I've either had to use a Google drive share link or spin up my own ftp server to get around that issue. Then with the ftp server, Chrome would flag the file as dangerous so the user would have to take extra steps to force Chrome to download. Lastly, which I barely remember, I think windows will make a big fuss about running the file since it's not signed. That also just requires extra steps in forcing windows to run it.

1

u/RebelSaul Jul 08 '20

I would use PyInstaller.

The resulting files are a littler larger but the process can look as simple as:

# In Terminal
pip install pyinstaller 
cd C\files\location
pyinstaller yourprogram.py 

Many packages are supported too.

1

u/knightwarrior911 Jul 08 '20

Does the last line of code make the program open using pyinstaller?

4

u/Beverneuzen Jul 08 '20

Looks like the last line converts it to a .exe

1

u/RebelSaul Jul 08 '20

It packages your python script into a build. I’m the folders created you have an executable file that runs your script.

2

u/[deleted] Jul 08 '20

Pyinstaller. Bam.

2

u/BfuckinA Jul 08 '20

Auto_py_to_exe is the simplest solution for this I've found

2

u/savaero Jul 09 '20

Guys, you can publish your code in a free google colaboratory notebook. Check it out...

2

u/Thecrawsome Jul 09 '20

You can share scripts in a REPL. A website that can host REPLs is repl.it.

1

u/JeamBim Jul 08 '20

Easiest is probably hosting on Repl.it and giving them the link

1

u/pokk3n Jul 08 '20

Pyinstaller is what we use. works great.

1

u/skellious Jul 08 '20

So you can make an EXE but another way you can do it is make an installer that installs python alongside your program in the program directory.

A third way is just to include python in a .zip. If you want to make it very easy, include a .bat file that is called runme.bat or something.

1

u/OnlySeesLastSentence Jul 09 '20

CX Freeze with the quickstart script. Make sure you manually put in any files and folders that you added that aren't part of python (images/music/etc)

1

u/Pervert_Chan Jul 09 '20

If you haven't done it yet then I'd like to suggest using cx-Freeze for creating app First install it by using command "pip install cx-Freeze" After installation - cxfreeze-quickstart It will ask for information about the app you want to create like description and version and will create a .py file (setup.py). Go to the terminal and run that setup.py file it will create the installable for you

1

u/iamdarius777 Jul 08 '20

Non relevant to what you're asking but the user needs to turn on less secure app usage or have an app specific password to use your program too right?

0

u/DrBobHope Jul 08 '20

Create a virtual environment. You can use pyinstaller, but I've always had issues with that (especially with newer versions of python).

13

u/[deleted] Jul 08 '20

[removed] — view removed comment

4

u/GrooseIsGod Jul 08 '20

I'm new to computer science stuff. Why?

8

u/PapaJohnX Jul 08 '20

Not so much computer science, but interpreted languages (like python) need to be run using interpreters (e.g. CPython) which converts the python code as we understand it -> Python bytecode -> low-level machine instructions. In compiled languages, the executable that is built is already machine code.

Some might disagree with what u/tux-deluxe w.r.t. compiled languages being more appropriate for creating a program that can be given to anyone. For example, lets say I create a C++ executable targeting Windows, my linux user friend cannot run my program natively in linux. However, if I create a python script, anyone who has a python interpreter can run my code, regardless of OS, architecture, etc.

1

u/Pervert_Chan Jul 09 '20 edited Jul 10 '20

Created a calculator sent it to my friend ;)