How do I package/distribute a Python 2.7 application with selenium? - python

I am fairly new to Python and I have developed a program that has a Tkinter GUI and uses selenium WebDriver to scrape information. I need to find a way to package all of this so that it can be used on other computers. I call three extra modules (selenium, openpyxl, BeautifulSoup) in my script. Is there a way to make this an executable file? Or will they have to call it through their command window? I have heard of a program called Advanced Installer, would this do what i want?
Thank you
I realize that my question is very broad, i just don't know where to begin. This application is only applicable to people within my company, so i don't want it to be available to others. I just want to be able to move all the necessary files to another computer and they can just click one button and go.

I encourage you to read how to package a python application. Typically what you want to do is release your application on the Python Packaging Index, so that your app is available as a Python module for other developers and users. Once you release your app on PyPI, you can go ahead and provide users with further options like easy install.
Note that releasing your app and making it available to the world is something that you'd certainly want to do.
The best way to package your application is by using distutils.
Since packaging a python app is a considerably broad topic, I won't be able to cover it in entirety here. However, here are a few guides to get you started:
Hitchhiker’s Guide to Packaging
Distributing Python modules
Guide to open-sourcing a python project
Finally, a good practice is to look at existing open-source Python projects and look at the way their setup script and the MANIFEST.in file is written. For doing that, you would need to understand the way the project is structured, which is important in realizing the correct way to package an application.
Hope this helps. All the best!

Related

How to make an installation interface for an application written in python

I'm working on an app, it's in the final stages of development, and I want to help design an installation interface for it, can anyone guide me?
This question largely depends on how thorough you want your installer to be, since installing a program basically translates to embed this complex piece of software on my pc for me to use hassle-free. To make a common one click installer, it will be a long process that is explained better here. I wont get too deep into this because there's many builders and tools that do the heavy lifting and simplify this process significantly. Just an example anyways, these can be built in Visual Studio and are formatted as .msi or .exe, or packaged up as self-extracting in InstallSheild. On a side note mac/linux installations work very differently. This is for Windows only!
If you are leaning towards making a much more indie-friendly installer that just moves a file to a location, a simple Windows VB program that is ran with admin privileges would do the trick. Simply fetch some files from a http source to a location on your computer, and VB supports a lot of very nice GUI elements to guide the user through the process.
As ironic as this sounds, you could even make the downloader in python and bundle that as an exe installer. It could utilize PyQT5, or just stay plain-jane command line and spit out the log as it executes.
At the end of the day, a bare-bones method would just be to host your application through a github repo and have clients git clone, or add your code to pypi.

How do I make a macOS app out of my Python program?

I've made this question because I had to go through the whole process of creating my own application using Apple's somewhat lacking documentation, and without the use of py2app. I wanted to create the whole application structure so I know exactly what was inside, as well as create an installer for it. The latter of these is still a mystery, so any additional answers with information on making a custom installer would be appreciated. As far as the actual "bundle" structure goes, however, I think I've managed to get the basics down. See the answer below.
Edit: A tutorial has been linked at the end of this answer on using PyInstaller; I don't know how much it helps as I haven't used it yet, but I have yet to figure out how to make a standalone Python application without the use of a tool like this and it may just be what you're looking for if you wish to distribute your application without relying on users knowing how to navigate their Python installations.
A generic application is really just a directory with a .app extension. So, in order to build your application, just make the folder without the extension first. You can rename it later when you're finished putting it all together. Inside this main folder will be a Contents folder, which will hold everything your application needs. Finally, inside Contents, you will place a few things:
Info.plist
MacOS
Resources
Frameworks
Here you can find some information on how to write your Info.plist file. Basically, this is where you detail information about your application.
Inside the MacOS you want to place your main executable. I'm not sure that it matters how you write it; at first, I just had a shell script that called python3 ./../Resources/MyApp.py. I didn't think this was very neat though, so eventually I called the GUI from a Python script which became my executable (I used Tkinter to build my application's GUI, and I wrote several modules which I will get to later). So now, my executable was a Python script with a shebang pointing to the Python framework in my application's Frameworks folder, and this script just created an instance of my custom Tk() subclass and ran the mainloop. Both methods worked, though, so unless someone points out a reason to choose one method over the other, feel free to pick. The one thing that I believe is necessary, is that you name your executable the SAME as your application (before adding the .app). That, I believe, is the only way that MacOS knows to use that file as your application's executable. Here is a source that describes the bundle structure in more detail; it's not a necessary read unless you really want to get into it.
In order to make your executable run smoothly, you want to make sure you know where your Python installation is. If you're like me, the first thing you tried doing on your new Mac was open up Terminal and type in python3. If this is the case, this prompted you to install the Xcode Command Line tools, which include an installation of Python 3.8.2 (most recent on Xcode 12). Then, this Python installation would be located at /usr/bin/python3, although it's actually using the Python framework located at
/Applications/Xcode.app/Developer/Library/Frameworks/Python3.framework/Versions/3.8/bin/python3
I believe, but am NOT CERTAIN, that you could simply make a copy of this framework and add it to your Frameworks folder in order to make the app portable. Make a copy of the Python3.framework folder, and add it to your app's Frameworks folder. A quick side note to be wary of; Xcode comes packaged with a lot of useful tools. In my current progress, the tool I am most hurting for is the Fortran compiler (that I believe comes as a part of GCC), which comes with Xcode. I need this to build SciPy with pip install scipy. I'm sure this is not the only package that would require tools that Xcode provides, but SciPy is a pretty popular package and I am currently facing this limitation. I think by copying the Python framework you still lose some of the symlinks that point to Xcode tools, so any additional input on this would be great.
In any case, locate the Python framework that you use to develop your programs, and copy it into the Frameworks folder.
Finally, the Resources folder. Here, place any modules that you wrote for your Python app. You also want to put your application's icon file here. Just make sure you indicate the name of the icon file, with extension, in the Info.plist file. Also, make sure that your executable knows how to access any modules you place in here. You can achieve this with
import os
os.chdir('./../Resources')
import MyModules
Finally, make sure that any dependencies your application requires are located in the Python framework site-packages. These will be located in Frameworks/Python3.framework/Versions/3.X.Y/lib/python3.x.y/site-packages/. If you call this specific installation of Python from the command line, you can use path/to/application/python3 -m pip install package and it should place the packages in the correct folder.
P.S. As far as building the installer for this application, there are a few more steps needed before your application is readily downloaded. For instance, I believe you need to use the codesign tool in order to approve your application for MacOS Gatekeeper. This requires having a developer license and manipulating certificates, which I'm not familiar with. You can still distribute the app, but anyone who downloads it will have to bypass the security features manually and it will seem a bit sketchy. If you're ready to build the installer (.pkg) file, take a look at the docs for productbuild; I used it and it works, but I don't yet know how to create custom steps and descriptions in the installer.
Additional resources:
A somewhat more detailed guide to the anatomy of a macOS app
A guide I found, but didn't use, on using codesign to get your app past Gatekeeper
A RealPython tutorial I found on using PyInstaller to build Python-based applications for all platforms

How do I download a 3rd party python module and integrate it with my python

I would like to be able to use the services that the Blockcypher module provides for my programme, however i have (at least i think) downloaded the correct module package but cant get it to integrate with my Python on my Computer. I am fairly new to python so I have no idea on where to even start tackling this problem.
Modules, regardless of where you've got hold of them, will be searched for in the sys.path. If nothing is found there, they will be looked up in the current directory.
When you download some code directly it will be a good first guess to place it in the directory of the script from where you are using the download. If it's just a .py-file, place it there. If it's an archive with a directory, then place the directory there (not the files).
Generally, you should prefer installing modules via a package manager such as pip or conda. Such package managers take care of placing modules properly for usage with your Python installation from wherever you will write your script. They also provide support for updating these modules to newer versions later.
Update: If you cannot make anything from this remarks, you should first read the section on modules in the Python tutorial, or even work thru the full tutorial or thru a good book (or any other ;) to get a smooth entry into the friendly world of Python programming.
Update (2023): The Dive Into Python link above is outdated, so here is the updated link to this great resource:
https://diveintopython3.problemsolving.io
I think it's still the best beginner's resource, but, well, here are many more:
https://wiki.python.org/moin/IntroductoryBooks

Including python and other files

All of the python I've written so far have been fine on my own computer, but now I'd like to send some programs to friends to have them test certain features. Suppose I wrote an application in python with wxpython. Assuming people I send code to will not have either installed, what is the best way to include both python, and the wxpython library so the other person isn't struggling to get it running? I've never had to do this at this point in my learning and would love some feedback!
Thanks.
You can create a bundle using py2exe and installer using NSIS and ship it as executable so that your friend will get the complete working executable. But mind you, this will increase the size of the file enormously and I have often found it easier to ask them to install via README.txt files.
There are lots of binary builders: py2exe, cx_freeze, bbfreeze, PyInstaller, GUI2Exe. I have a whole slew of articles on these:
http://www.blog.pythonlibrary.org/2010/08/31/another-gui2exe-tutorial-build-a-binary-series/
http://www.blog.pythonlibrary.org/2010/07/31/a-py2exe-tutorial-build-a-binary-series/
http://www.blog.pythonlibrary.org/2010/08/19/a-bbfreeze-tutorial-build-a-binary-series/
http://www.blog.pythonlibrary.org/2010/08/12/a-cx_freeze-tutorial-build-a-binary-series/
http://www.blog.pythonlibrary.org/2010/08/10/a-pyinstaller-tutorial-build-a-binary-series/
Unless they are going to develop with Python too, then I don't see any reason for them to want to install a bunch of multi-megabyte installers versus your own. You can read about how to use Inno Setup to create an installer here:
http://www.blog.pythonlibrary.org/2008/08/27/packaging-wxpymail-for-distribution/

Installing a Python program on Linux

I wrote a Python program. I would like to add to it an installation script that will set up everything necessary - like desktop icon, entry in the menu, home directory file, etc.
I'm working on Linux (ubuntu). When a Python program is installed, what needs to happen in general? I know that it probably depends on the nature of the program.
Can you give me some general ideas? Or, point me in the right direction? I have no idea how to look for this on Google.
Thanks
If it's a Python program you're trying to package, you should consider using its 'standard' distribution framework distutils. I can't replicate the entire document here but I'd recommend that you read it. Once you're done with that, check out the Hitchhikers guide to packaging which contains details on distribute - the extensions to distutils that allow you to package and distribute more effectively.
You could create an rpm easily using checkinstall. Search for checkinstall in google and download it. It will allow you to create an rpm and set the options.
For Ubuntu if you want it to be easily distributable to other Ubuntu users it'll have to be packaged properly, which is no simple task. You might want to consult their Packaging Guide for more information.
Otherwise, generally speaking there are a few standard packaging options for Python. Setuptools is popular, but becoming reviled lately. Read James Bennett's blog post "On Packaging" for a decent in-depth look into the ups and downs of the Python packaging world.
How a program is launched and placed in the menu is determined by a .desktop file (you can read the specification or just look at some examples from /usr/share/applications). Properly installing a program (placing all files in the right directories and so on) requires either making a package like a deb or rpm, or you could use something like distutils or setuptools.
It may also help to just look at some (open source) examples of Python programs for Linux.

Categories