For Linux/Mac:
1. Move the Script to a Global Directory: Commonly, /usr/local/bin
is in the system’s PATH. You can move your script there.
sudo mv <script>.py /usr/local/bin/<script>
2. Make the Script Executable:
sudo chmod +x /usr/local/bin/<script>
#!/usr/bin/env python3
This tells the system to use Python to execute the script.
Now, you can simply run <script>
from any directory in your terminal.
For Windows:
1.Add Script Directory to PATH:
- First, place your script in a dedicated directory, e.g.,
C:\scripts
. - Then, add this directory to your system’s PATH:
- Right-click on ‘This PC’ or ‘Computer’ on the desktop or in File Explorer.
- Click on ‘Properties’.
- Choose ‘Advanced system settings’ on the left.
- Click on the ‘Environment Variables’ button.
- In the ‘System Variables’ section, scroll down and select the ‘Path’ variable, then click ‘Edit’.
- Click ‘New’, then paste in the path to the directory where your script is located, e.g.,
C:\scripts
. - Click ‘OK’ to close each of the windows.
- First, place your script in a dedicated directory, e.g.,
2.Rename Script for Convenience: Rename your script from <script>.py
to <script>.pyw
. This will allow you to run the script without popping up a command prompt window.
Now, you can simply run <script>.pyw
from any directory in your Command Prompt or PowerShell.
Remember, for the script to work globally, any external Python libraries it uses (like requests
) must be installed globally (i.e., they should be accessible from any location on your computer). If you’re using virtual environments, make sure the required libraries are installed in the global environment or in all virtual environments where you plan to use the script.