Home > database >  Running Python script from anywhere on windows platform
Running Python script from anywhere on windows platform

Time:03-01

I am keeping a folder inside my windows machine(my_codes) where I keep all my codes. I have added this folder path to my path env variable inside windows path.

Now when I call the script from somewhere I am not able to pass the argument correctly.

(base) C:\Users\abc>test.py abc.csv
print_test
C:\Users\abc\my_codes\test.py
Traceback (most recent call last):
  File "C:\Users\abc\my_codes\test.py", line 4, in <module>
    print(sys.argv[1])
IndexError: list index out of range

The python code is as below. I have saved it as test.py inside C:\Users\abc\my_codes

import sys      
print('print_test')
print(sys.argv[0])
print(sys.argv[1])

This works perfectly inside linux but unable to get it work in windows so far.

This works inside windows too if I call it this way.

(base) C:\Users\abc>python my_codes/test.py abc.csv
print_test
my_codes/test.py
abc.csv

However I do not want to call by specifying the full path each time to the python file.

CodePudding user response:

Huh. It looks like sys.argv only contains [0] and [1] is not defined. Is there only 4 lines of code?

CodePudding user response:

I created a function in powershell as below:-

function test {
     python C:\Users\abc\my_codes\test.py $args
}

(base) C:\Users\abc>test abc.csv
print_test
my_codes/test.py
abc.csv

and I am able to call test abc.csv. It serves my purpose but I would have loved to get it done in more proper way.

  • Related