mountainclimber11
mountainclimber11

Reputation: 1400

How to run python scripts without specifying their full path

From the cmd window I have to do this every time I run a script:

C:\>cd C:\Users\my name\AppData\Local\Programs\Python\Python37

C:\Users\my name\AppData\Local\Programs\Python\Python37>python "C:\\Users\\my name\\AppData\\Local\\Programs\\Python\\Python37\\scripts\\helloWorld.py"
hello world

How can I get away from having to paste in all of the paths?

I tried this and a few other things: https://www.youtube.com/watch?v=Y2q_b4ugPWk

thanks!

Upvotes: 2

Views: 2884

Answers (2)

Amit Naidu
Amit Naidu

Reputation: 2638

There is a designated directory where you can put your .py scripts if you want to invoke them without specifying the full path.

Setting this up correctly will allow you to run the script simply by invoking the script name (if the .py extension is registered to the interpreter and not an editor).

Windows

If you have a per-user python installation - which is the installer default - the directory is:

%LOCALAPPDATA%/python/python39/Scripts

Adjust version number as needed.

If you have a system-wide all-user installation, the directory is:

%APPDATA%/python/python39/Scripts

Configuring PATH automatically

The Windows python installer includes an option to automatically add this directory (plus the python interpreter path) to your PATH environment variable during installation. Select the checkbox at the bottom or use the PrependPath=1 CLI option. Python installer dialog showing add-to-path checkbox

If python is already installed, you can still use the installer to do this. In Control Panel, Programs and Features, select the python entry and choose "Uninstall/Change". Then choose "Modify" and select the "Add Python to PATH" checkbox.

Alternatively, if you want to add it manually - search for the Edit environment variables for your account in Windows 10. Edit the PATH variable in that dialog to add the directory.

Linux

~/.local/bin

Upvotes: 0

r.ook
r.ook

Reputation: 13878

You need to pay attention to the current working directory of your python interpreter. It basically means the directory you are currently in where you execute the python interpreter, and it relies on that path to look for your script passed in. If you're inside the script already, you can easily check with os.getcwd() method.

In your case, you could have easily done this instead:

C:\Users\my name\AppData\Local\Programs\Python\Python37>python "scripts\helloWorld.py"
hello world

Since your current working directory is C:\Users\my name\AppData\Local\Programs\Python\Python37, you just need to give it the relative path scripts\helloWorld.py.

The current working directory can be easily visualized like this:

# cwd.py
import os
print("Current Working Directory is " + os.getcwd())

And then when you run the scripts:

C:\Users\MyUserName\Documents>python cwd.py
Current Working Directory is C:\Users\MyUserName\Documents

C:\Users\MyUserName\Documents\Some\Other\Path>python cwd.py
Current Working Directory is C:\Users\MyUserName\Documents\Some\Other\Path

Note in any case, if the cwd.py was not in the current working directory or in your PATH environment variable, python interpreter would complain it couldn't find the script (because why should it know where your script is stored?)

If you insist in adding the environment variable though, you will need to add the directory to your PATH or PYTHONPATH... though I have a feeling \Python37 is already under there.

Upvotes: 1

Related Questions