I have a python project with lots of other.py files it import from its sub folders and txt files as well
I want to share this project but only as one executable file so that Noone can see the code or the text files etc...
Is there any way to do so
If not how else can I share the project without letting anyone being able to check the code of the.py files
You can user PyInstaller (https://www.pyinstaller.org/) to build a stand-alone executable.
Related
I am learning Python and managed to create my first project. Now I have several files and folders in my project folder which I need, to let my program (tkinter based GUI with some selenium code) run correcty.
I have not found a way to convert my main.py file to an .exe file while incuding my complete project folder.
Is there any advice which module I could use to achieve that?
Is ther a way to just pack the complete project and generate a executable "folder" which I can share with friends?
Make sure your main.py kicks off like this:
if __name__ == '__main__':
# my code
then you should be able to call pyinstaller like this:
pyinstaller main.py
If you have any data files you can add them using the command line or using a spec file. Here's the docs.
I have converted a python file with seleniumto an exe using pyinstaller using this guide. I have followed the steps and have one singular exe file in the dist folder of my main project folder. My question is, how would I share this file because doesn't it depend on having selenium installed? What about the build folder in the main directory? Someone who has experience with this type of thing please can you help. I can answer any further questions in the comments, thanks.
When you use import statements in your script, PyInstaller automatically detects the modules imported and include them to the compiled EXE. It can detect a lot of modules, and selenium is between them (full list)
I have a python project called deduplication which works with the help of multiple scripts. As in deduplication.py makes use of readCSV.py and dbmanager.py. So how can I turn the deduplication.py into an executable if it is dependent on the other 2 .py files?
just add an .exe to your project file
Not a major issue but just an annoyance I've come upon while doing class work. I have my Notepad++ set up to run Python code straight from Notepad++ but I've noticed when trying to access files I have to use the full path to the file even given the source text file is in the same folder as the Python program being run.
However, when running my Python program through cmd I can just type in the specific file name sans the entire path.
Does anyone have a short answer as to why this might be or maybe how to reconfigure Notepad++?
Thanks in advance.
The problem is that your code is assuming that the current working directory is the same as the script directory. This is not true in general. Of course it is true if you're in a cmd window, and you cd to the script directory before running it.
If you don't want to rely on that (e.g., because you want to be able to run scripts from Notepad++, or directly from Explorer), what you want to do is use the script directory explicitly. For example:
import os
import sys
scriptdir = os.path.abspath(os.path.dirname(sys.argv[0]))
with open(os.path.join(scriptdir, 'myfile.txt')) as f:
# etc.
If you have a ton of files that your scripts reference in a ton of places, it might be better to explicitly set the working directory. Just add one line:
os.chdir(scriptdir)
For anything beyond quick&dirty scripts, it's usually better to build an installable package and use pkg_resources to access the data files. Read the Tutorial on Packaging and Distributing Projects for more details. But as long as you're only hacking up scripts to help you maintain your specific system, the scriptdir solution is workable.
In the properties of the shortcut that you use to start Notepad++, you can change its working directory, to whichever directory you're more accustomed to starting from in Python. You can also begin your python program with the appropriate os.chdir() command.
Hi!I made a chess engine in python which i then compiled to .exe using py2exe. The problem is that it doesn't look very neat when i have all the strange files gathered together in the same folder (dist). I'd like to make a new folder inside the dist folder that contains all the helper files, so all my dist folder contains is the folder holding the helper files and the main launch application. However, i can't simply copy the helper files to a new folder, as the computer doesn't find them then and raises an error.
How can it be solved? Also, i'm using inno setup to make an installation, but i can't figure out how to find a solution there, either.
Thank you very much!
There is a feature in the configuration of py2exe that allows you to bundle all the Python files in a single library.zip file. That would considerably reduce the amount of files in the root directory, but there will still remain some files, regardless of all that.
These files are generally DLL files, at least from what I saw with GUI applications. You cannot remove these, because they are required to launch the application. A workaround to this problem is to create a batch file that will run the actual program which can be in child directory. The point is that these files should either be in the same directory as the executable, or the current working directory, or a path in the PATH environment variable. At least it's the case of most of these. Another approach might be a batch file which will modify the PATH variable or cd to another directory and run the file afterwards
I never tried to do it, so it might break some things for you. Anyway, IMO the best approach is to create an installer and add shortcuts and you won't have to bother with the user messing with these files.
Try using pyinstaller instead. It's easy to use, and will compile your PythonLib and all necessary python files to a stand alone EXE. So you don't have to worry about the having a mess of files in your dist file. (just one single exe).
And if you have other external files, such as databases, text files, csv's. etc... you can set them up to deploy in exactly the fashion you want from the inno setup [Files] section.
I wrote a detailed explanation on this yesterday, so check out this link:
https://stackoverflow.com/a/13259452/1339024
--Edit--
*Make sure you use pyinstaller 1.5 , as the 2.x version doesn't exactly work the same