Example of my personal use:
- convert the selected image(s) to another image format with 2 clicks using Pillow (jpg|png|webp)
- create a python virtual environment in the current folder. Copying boilerplate files/folders to quick start a project
- run the current selected python script and using venv if found
----
XYplorer and Python
Using Python can boost your XYplorer experience, even in the portable version. No need to install Python on your machine—just use the embeddable version.
0. Requriements
- XYplorer Portable
1. Download Embeddable Python
Go to https://python.org
and download the zip file:
Code: Select all
Windows embeddable package (64-bit)
https://www.python.org/ftp/python/3.12. ... -amd64.zip
2. Unzip Inside a Folder in Your XYplorer Installation Folder
You can name the folder whatever you want, but in this guide, we use python_embed as an example.
Code: Select all
xyplorer_full_noinstall/
├── Data/
└── python_embed/
PIP is a package manager for Python packages, or modules, if you like.
Download: https://bootstrap.pypa.io/get-pip.py
Saved location
Code: Select all
xyplorer_full_noinstall/
├── Data/
└── python_embed/
├── get-pip.py
├── ...
├── python.exe
Open the Command Prompt in the python_embed folder and type:
Code: Select all
> python get-pip.py
Code: Select all
> python -m pip install SomePackage
Tkinter is a Python library that can be used to construct basic graphical user interface (GUI) applications.
For this step to work, you need to have already installed the same Python version on your machine using the Windows installer (64-bit) executable.
The reason is that the .zip version doesn't contain the tkinter module, and you need to manually copy certain folders and files.
Copy:
- tkinter/ (folder) -> python_embed/Lib/site-packages/tkinter/
- tcl/ (folder) -> python_embed/tcl/
- _tkinter.pyd -> python_embed/
- tcl86t.dll -> python_embed/
- tk86t.dll -> python_embed/
- zlib1.dll -> python_embed/
Code: Select all
xyplorer_full_noinstall/
├── Data/
└── python_embed/
├── Lib/
│ └── site-packages/
│ └── tkinter/
├── _tkinter.pyd
├── tcl86t.dll
├── tk86t.dll
├── zlib1.dll
├── ...
├── Scripts/
└── tcl/
Depending on the version you downloaded, the file name will be different.
ver 3.11 is python311._pth
ver 3.12 is python312._pth
...
The file is located in the root of the python_embed folder.
Code: Select all
xyplorer_full_noinstall/
├── ...
└── python_embed/
├── python312._pth
├── ...
Before
Code: Select all
python312.zip
.
# Uncomment to run site.main() automatically
#import site
After
Code: Select all
python312.zip
Lib/site-packages
Scripts
.
# Uncomment to run site.main() automatically
#import site
6. Creating XYplorer and Python Script
test.py
Code: Select all
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--folder")
args = parser.parse_args()
if args.folder:
print(f"python: arg -f: {args.folder}")
else:
print(f"python: arg -f not found")
Code: Select all
function main(){
// <curpath> current folder
// <curitem> current item
$python = """<xypath>\python_embed\pythonw.exe""";
$script = """<xypath>\Data\Scripts\test.py"" -f ""<curpath>""";
msg( runret("$python $script") );
}
main();
Code: Select all
xyplorer_installation/
├── Data/
├── Scripts/
├── run_test_py.xys
└── test.py
Using pythonw.exe avoids opening the console window every time you run the script.
When creating your .xys script, surround any paths with "" because spaces in paths can generate errors.
7. Add Custom Button to Activate the Script
Add a button to toolbar and edit
On Left Click:
Code: Select all
Load "run_test_py.xys"
Note about import
Remember that the path to modules and packages should be relative to the python_embed folder, as defined in python3xx._pth. When pip is used for installation, packages are installed in the Lib/site-packages folder, which is already included in python3xx._pth in this guide.
Example
Code: Select all
xyplorer_full_noinstall/
├── ...
└── python_embed/
├── ...
└── my_projects/
└── myapp/
├── module_name.py
└── test.py
Code: Select all
import my_projects.myapp.module_name
def main():
module_name.foo()
if __name__ == "__main__":
main()
Congratulation, you have a portable XYplorer with in python support