How To: Exporting a PCK with compiled Python for distribution #381
Replies: 2 comments 4 replies
-
        Extra noteIf you want to save a little more space, you can also remove the  
 Everything's already built and run through the  You can also remove any unnecessary modules from the  These things should be decided on a per-project basis, depending on whether you're distributing your project and/or scripts open-source, so they're not reflected in the gist above, but just know the options are there :)  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         i hope this is be easy in the future ./Godot_v3.5.3-stable_x11.64 --path pythonTest/ --export "Linux/X11" /home/archkubi/Godot/newexport/ Pythonscript 0.50.0 (CPython 3.8.5.final.0) at: add_message (editor/editor_export.h:254)  | 
  
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
There have been a lot of questions the last couple years floating around misc forums about how to package a Godot-Python (PythonScripts) project for distribution, and I rarely see much in the way of answers.
Since there's no Wiki, I thought I'd drop a quick primer on how I've been accomplishing the task in hopes of helping a few lost souls -- but feel free to migrate this elsewhere!
In the end you should end up with the following files/directories:
.pycpython bytecode files)The steps should be about the same on any OS.
I'll attempt to describe them in full detail below.
Here's a quick gist I wrote up to automate the whole task for Linux.
Anyone's welcome to clean it up, update it for macOS, or translate it to a Windows
.batfile to suit their needs.If so, share and share alike :)
Required Tools
For our purposes here, I will assume Godot and GodotPckTool are installed and available on your system PATH -- respectively named
godotandgodotpcktool. If you're on Windows, you'll probably need to tack on a.exeextension to associated calls.Step 1 - Configure the Project for Export
Open your project and make sure ALL of your Python scripts are in a project-root-level directory called "Scripts".
If they're not already, you should be able to safely move them. Godot will pick up the change.
Then go to the
Project>Export...menu. If you don't already have an export template installed, clickAddat the top of the new window and select (or download) an appropriate template for your target operating system.Once you have a template installed/selected, ensure
Embed PCKis NOT checked and then you can cancel out.At this point, assuming your project already runs correctly, you don't even really need to keep Godot open anymore if you want to do everything from the command line. If you prefer a graphical user interface, leave Godot open (for running exports) and open your favorite file explorer.
Step 2 - Ensure Python Won't Be Added to the PCK
Python won't run from inside the PCK archive, nor will it run any scripts within said archive -- we'll hack around the scripts in a minute, but first we want to make sure Python won't be added to the PCK file.
Navigate to your project directory, then
addons/pythonscript. Ensure there's a file called.gdignorein there. It doesn't need any content. It just tells Godot to ignore the directory when looking at project files.Step 3 - Export Your Project
If you're doing this from Godot, return to the
Project>Export...menu and click theExport Project...button at the bottom.Make sure to export to another directory. For example, if your project directory is named "HelloWorld", make another directory outside of it called "HelloWorldExport", and then export your game files there.
If you prefer to do it on the command line, your export command should look something like this:
(Runnable)trailing bit).--export-debuginstead of--exportto export a debug buildYou should end up with 3 files:
libpythonscriptlibrary file you probably don't need (I always delete it on Linux)Step 4 - Bring in your Portable Python Installation
In your export directory, along side the game binary, create a new directory called
addons.Inside the new directory, create another directory called
pythonscript.In a new window, navigate to the
addons/pythonscriptdirectory in your project, and copy theLICENSE.txtfile and the appropriate platform directory matching your distribution OS (in my case, x11-64 for Linux) over to theaddons/pythonscriptin your export directory.Step 5 - Repack the PCK - Trick Godot ;)
The important thing to note here is that Godot never actually looks at the content of resource files when it starts up. It checks to make sure all the files referenced by the project are present, but it doesn't actually read them until they're requested. You're going to take advantage of that and give it a bunch of empty dummy files to act as script placeholders in the PCK.
Python will read the -real- script files later when the addon starts up -- we'll get to those in the next step.
Create a
Scriptsdirectory in your export directory along side the game binary.To be honest, this part's kind of annoying if you're not automating it with a script -- but we do what we must.
Inside the new
Scriptsdirectory, create a new empty file for each*.pyPython script in your project'sScriptsdirectory, and name them exactly the same. These empty dummy files are going to replace the scripts currently in the PCK.Once you're sure you have a precisely named empty dummy file for each of your Python scripts, open a terminal,
cdto your export directory, and run the following command to replace the Python scripts in the PCK with your dummy copies -- Note, I'm going to call itHelloWorld.pckhere -- replace it with your actual PCK file's name.If you're curious (and you should be), in long form, that's:
--action repack--exclude-regex-filter "/Scripts/*.py"--action add "Scripts/"If you'd like to verify the PCK file at this point, you can do so by extracting it using the following command. Just remember to delete the
extracteddirectory when you're done.Step 6 - Bring In the Real Python Scripts
Now that you've tricked Godot's PCK loader and eliminated that wasted space, time to take advantage of Python's better nature :)
When the Godot-Python addon fires up Python, it knows nothing of the PCK file.
It wants to see those scripts where it expects them (and where it can compile them), in an actual
Scriptsdirectory.Assuming everything went well in Step 5 (you did verify the PCK file, right? ;) then you can go ahead and delete those dummy files in the export
Scriptsdirectory you created earlier.Copy in all of the real
*.pyfiles from your project'sScriptsdirectory to the export'sScriptsdirectory.Step 7 - Compile Your Scripts and... BE DONE!
When Python loads a
*.pyfile, it compiles it to bytecode before running it the first time. On subsequent runs, it looks to see if it has a bytecode file that isn't older than the*.pyfile itself, and, if so, just goes ahead and runs it. As a matter of fact, so long as you name the bytecode file correctly, it doesn't even care if there's a*.pyfile at all!Regardless of your distribution intentions, you don't want players to have a slow first start (and bad first impression), so you should precompile the bytecode for them.
Note: This must be done separately for each release on each operating system -- they don't share the same bytecode.
In a terminal,
cdinto the export'sScriptsdirectory and run one of the following:(Note: the first argument to python is a hyphen followed by two capital letter 'o' -- this enables the optimizer)
Linux:
macOS:
Windows:
You should now see an extra
__pycache__directory containing the compiled*.pycbytecode files inside yourScriptsdirectory.Now, if you're releasing your game open source, and intentionally trying to keep your scripts available to make it easier to mod, then you can stop here -- you're all done and you can skip the rest :)
But if not... if you want to obfuscate your code, get rid of all those comments and docstrings, and make it significantly more difficult for players to mod your scripts (but not impossible!), there's just one more thing to do.
Delete all of the
*.pyfiles from your export'sScript'sdirectory, and then copy all of the compiled*.pycbytecode files from the new__pycache__directory down into theScriptsdirectory, and delete__pycache__.The files should look something like
Hello.cpython-38.pyc-- the middle string may vary slightly from system to system, but it just designates the compiler used to generate the file. Rename all of the*.pycfiles, removing the compiler string from the middle.Example
Hello.cpython-38.pyc->Hello.pyc.That's it. You're done. 2 Files, 2 Folders.
Run the game binary and make sure everything's working as planned.
If so, your dist version is all set to go :)
Beta Was this translation helpful? Give feedback.
All reactions