使用便携Python运行带命令行参数的脚本时出现ModuleNotFoundError及PYTHONPATH失效问题求助
Hey there! Let's break down what's going on here and get your script working with the portable Python setup.
The Root Cause
Portable Python distributions are built to be self-contained, which means they often ignore system-level or batch-file-set environment variables like PYTHONPATH by default. Some versions even reset the sys.path to only include their own internal libraries and the script's directory—so your manually set PYTHONPATH entries (Folder1 and Folder2) don't make it into the Python runtime's path list.
Another possible issue: if you're using relative paths for Folder1/Folder2 in your PYTHONPATH, the portable Python might be running from a different working directory than your batch script, making those relative paths unresolvable.
Easy Fixes for Beginners
Here are three straightforward solutions, ordered by how easy they are to implement:
1. Add Paths Directly in Your Python Script (Most Reliable)
Skip relying on environment variables entirely by manually adding Folder1 and Folder2 to sys.path at the very top of your target Python script. This works no matter which Python runtime you use:
import sys import os # Get the absolute path of the current script's directory script_folder = os.path.dirname(os.path.abspath(__file__)) # Add Folder1 and Folder2 using their absolute paths sys.path.append(os.path.join(script_folder, "Folder1")) sys.path.append(os.path.join(script_folder, "Folder2")) # Rest of your script goes here...
This ensures Python can always find the modules in those folders, even if the working directory changes or the portable Python ignores PYTHONPATH.
2. Use Absolute Paths in Your Batch Script
If you prefer keeping the path setup in the batch file, update your PYTHONPATH to use absolute paths instead of relative ones. Use %~dp0 to get the folder where your batch script is located:
:: Set PYTHONPATH to absolute paths of Folder1 and Folder2 Set PYTHONPATH=%~dp0Folder1;%~dp0Folder2 Set File=%~1 :: Replace with your portable Python's actual path .\portable_python\python.exe %File% --arg1=value --arg2=value2
%~dp0 gives you the full path to the batch script's directory, so Folder1/Folder2 are referenced correctly no matter where the user runs the batch file from.
3. Auto-Add Paths to Portable Python
If you want a "set it and forget it" solution, create a sitecustomize.py file and place it in your portable Python's Lib/site-packages folder. This file runs automatically every time Python starts, so you can add your paths here:
import sys import os # Get the portable Python's executable directory python_exe_dir = os.path.dirname(sys.executable) # Assuming Folder1/Folder2 are in the same root as your portable Python folder root_dir = os.path.dirname(python_exe_dir) sys.path.append(os.path.join(root_dir, "Folder1")) sys.path.append(os.path.join(root_dir, "Folder2"))
Just adjust the path logic to match how you've structured your compressed package (e.g., if Folder1 is inside the same folder as the batch script, tweak the path accordingly).
内容的提问来源于stack exchange,提问作者An29

