3

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 want to have an ability to use this utility from anywhere in the filesystem. For example if I am currently in some folder I want to run something like

 >python util1.py

And get the required result in the current folder.

How to do it?

If it's not possible is there a way to create one module with .py scripts and .template files and then take it with me anywhere?

If I put it into PYTHON_HOME aka D:\Python27 it shows error

python: can't open file 'util1.py': [Errno 2] No such file or directory

If I put it into C:\Windows it doesn't work either.

lapots
  • 12,553
  • 32
  • 121
  • 242
  • Try to edit an ini file and put the location of your required file in it .And use copy method to the current path when the program done delete them if necessary . – lqhcpsgbl Jan 16 '15 at 08:00
  • I think we need more details. Where is your current approach failing? Does the Python interpreter fail to start? Does your program fail to start? Do the `import`s fail? Is the output going into the wrong directory? – Tim Pietzcker Jan 16 '15 at 08:00
  • @TimPietzcker, no it is not failing as a program itself. If I copy my scripts to any folder and run it from there - it works fine. But I don't like that approach - copy multiple (6) scripts with additional (3) files in order to run scripts. – lapots Jan 16 '15 at 08:02
  • 1
    I might have totally forgotten Windows, but what happens when you place your script in some folder that is on %PATH% (say C:\Windows), and just execute with the filename (ie not using Python interpreter explicitly to invoke your script) ? Of course, your script will need to figure out where to look for dependent resources – UltraInstinct Jan 16 '15 at 08:04

3 Answers3

1

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

James Lemieux
  • 720
  • 1
  • 9
  • 26
  • I set it manually in `environment variables` window and it does not work either... – lapots Jan 16 '15 at 08:38
  • 1
    it works if you run 'util1.py' on commandline directly instead of 'python util1.py' as Thrustmaster mentioned above. – D-rk Jan 16 '15 at 08:52
  • It works if I copy my scripts to `windows` folder. But it's bad solution I think... – lapots Jan 16 '15 at 08:56
  • @user1432980 Since your script relies on other scripts, you have to make sure they are also in your path. If not, have your script access them via their full path, otherwise python will still not know where to look even though `util1.py` is in your path. – James Lemieux Jan 16 '15 at 18:58
0

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).

with open(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.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

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

Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34