Windows 7系统中如何设置PYTHONSTARTUP环境变量?取值是什么?
Alright, let's break down exactly how to set up the PYTHONSTARTUP environment variable on Windows 7, plus what value it should hold.
First: Create Your Initialization File
Unlike Unix systems that use .pythonrc.py by default, Windows doesn’t have a pre-defined filename for this. You can name it whatever you want—most folks stick with something like pythonrc.py for consistency.
Save this file somewhere easy to find, like your user folder: C:\Users\YourActualUsername\pythonrc.py (replace YourActualUsername with your Windows login name). Inside the file, you can add any startup code you want—here's a quick example:
# Example pythonrc.py content import sys import os # Add a custom scripts folder to your PYTHONPATH sys.path.append("C:\\Users\\YourActualUsername\\PythonScripts") # Make the interactive prompt a bit friendlier sys.ps1 = ">>> "
Second: Set the PYTHONSTARTUP Environment Variable
Now let's link this file to the environment variable:
- Right-click Computer (or "My Computer" on your desktop) and select Properties.
- On the left side of the window, click Advanced system settings.
- In the System Properties window that pops up, go to the Advanced tab and click Environment Variables.
- Choose whether you want this variable to apply only to your account (use User variables) or all users on the PC (use System variables). Click New to create a new variable:
- Variable name: Type
PYTHONSTARTUPexactly (case doesn’t matter on Windows, but it’s good practice to match the standard). - Variable value: Paste the full path to your initialization file, e.g.,
C:\Users\YourActualUsername\pythonrc.py.
- Variable name: Type
- Click OK on all open windows to save the changes.
Third: Verify It's Working
To make sure everything's set up correctly:
- Close any open Command Prompt windows (environment variable changes don’t take effect in already open terminals).
- Open a new Command Prompt and type
pythonto launch the interactive interpreter. - Test your startup code—for example, if you added a folder to
sys.path, runprint(sys.path)and check if your custom folder is listed. If you changed the prompt, you'll see your new>>>right away.
A quick note: If it's not working, double-check that the path in PYTHONSTARTUP is 100% correct—typos in file paths are the most common issue here.
内容的提问来源于stack exchange,提问作者user3721902

