4

I'm using windows cmd to run my python script. I want to run my python script withouth to give the cd command and the directory path. I would like to type only the name of the python script and run it.

I'm using python 2.7

5
  • are you using python 2 or 3? Commented Sep 23, 2016 at 14:20
  • 1
    Add the commands to run the script inside a batch file and then put that batch file somewhere in the PATH. Then just run the batch file to run the script. Commented Sep 23, 2016 at 14:21
  • superuser.com/questions/737542/… Commented Sep 23, 2016 at 14:22
  • 1
    python 2. (I edited my question) Commented Sep 23, 2016 at 14:22
  • you want to run a python script with the startup? Or just run the python scripts with python script_name.py from any where in the command prompt? Commented Sep 26, 2016 at 10:43

3 Answers 3

3

Create batch file inside the script's directory.

@echo off
echo.
python %~dp0\<script-name>.py %*

Put the above lines inside of it. And change according the script you want to run.

"%~dp0" is unique variable witch returns batch file's directory.

"%*" are the arguments passed through.

os.chdir(os.path.dirname(sys.argv[0]))

Also put the above line inside the script. The code above changes your python script's working directory to it's own directory. "sys.argv[0]" returns path of the script itself.

And add your script to Enviroment Variables. And run from command prompt using the batch file's name.

Sign up to request clarification or add additional context in comments.

3 Comments

"sys.argv[0]" always returns path of the script itself." no, it doesn't.
"Also put the above line inside the script. The code above changes your python script's working directory to it's own directory." why would you do this???
Changed the "sys.argv[0]". For the working directory, people who use relative paths while managing file operations generally mean the directory the script is in. But when script is called from command prompt it sets it's working directory to the place its called from. I used it for my own needs. Just added to the post if he needs it. It's obviously not required. Your comment seems agressive but i am guessing you are just guiding me to post the reason. Sure.
2

Make sure .py files are associated with the Python launcher C:\Windows\py.exe or directly with e.g. 'C:\Python27\python.exethen edit yourPATHEXTenvironment variable using (System Properties) to add;.PY` at the end. You can now launch Python files in the current directory by typing their name.

To be able to launch a given Python script from any directory, you can either put it in a directory that's already on the PATH, or add a new directory to PATH (I like creating a bin directory in my user folder and adding %USERPROFILE%\bin to PATH) and put it there.

Note that this is more a "how do I use Windows" question rather than a Python question.

Comments

-1

1.Go to Environmental Variables > system variable > Path > Edit

2.It look like this

Path C:\Program Files\Java\jdk1.8.0\bin;%SystemRoot%\system32;C:\Program Files\nodejs\;

3.You can add semicolon (;) at the end and add C:\Python27

4.After adding it look like this

C:\Program Files\Java\jdk1.8.0\bin;%SystemRoot%\system32;C:\Program Files\nodejs\;C:\Python27;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.