Skip to content Skip to sidebar Skip to footer

Run Python Scripts From Anywhere In The Filesystem In Windows

I've implemented some utility for my needs to simplify the development using Python. It requires multiple .py files and some additional .template files - renamed .txt file. I wan

Solution 1:

Run setx path "%path%;C:\path\to\util.py" and you should be good to go.

Solution 2:

As for your scripts, put them in a directory and add that directory to your PYTHONPATH environment variable.

For example,

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

I'm assuming that the .template files are read by your script? In that case, hardcode their location in your script (if you don't, the script will look for them in your current directory).

withopen(r"C:\My_templates\foo.template") as footemp:
    # do something

Now if you cd to any directory and run python.exe util.py, Python will find util.py because it's in the Python path. Its imports will also use that path and therefore succeed as well. If your script opens template files, it will find them because their location is specified. And if your script writes files, it will do so in the current directory.

Solution 3:

You can put in C:\Windows a util1.bat file which contains: python path_to_util1\util1.py %*

Post a Comment for "Run Python Scripts From Anywhere In The Filesystem In Windows"