Solved Using PATHS in the command line in Windows 7

winspear1

New member
Local time
2:52 PM
Messages
5
Hi there,
I have been looking for a simple way to begin to understand the command line in windows. I'm trying to import python code from a folder, and at the moment I can only get my code to run if I save my folder in the default c:\Users\Jamie\<full path>. My question is in two parts
1. How can I run simple scripts from anywhere else (say, my documents)
2. Can I edit paths somehow such that typing % python <script name> will run my script, from the default C:\Users\Jamie, or otherwise?
Sorry if the questions themselves contain errors in understanding, i haven't used the Windows command before
 

My Computer

Computer type
PC/Desktop
OS
64 bit Windows 7
so you want to do this in command prompt?

python c:\whatever\subfolder\somescript.py


Where is python.exe located?

You want it to work for your account only I assume (?)
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
"c:\user\Jamie\<full path>\python" c:\whatever\subfolder\somescript.py

does work?
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
Hi Kaktusoft,
Yes, "c:\user\Jamie\<full path>\python" c:\whatever\subfolder\somescript.py does work. However, I have a user guide which is prompting me to run scripts using % commands, such as %python somescript. Is there a way to configure Windows to accept %(sometext) as a shortcut for the folder containing my python files?

I can write out the full path each time, but that is time consuming.
 

My Computer

Computer type
PC/Desktop
OS
64 bit Windows 7
You want that this works?

python c:\whatever\subfolder\somescript.py
or
%python% c:\whatever\subfolder\somescript.py

or???? Give me an example please.

Where is python.exe located?
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
I would like to be able to type %python as a shortcut to pythonFiles, where i keep all my scripts, such that i can type
%python script1 to run script 1, or %python script 99 to run script 99.
Basically I want to shortcut c:\Users\Name\pythonFiles to avoid typing that every time
Is there any way that can be done?
 

My Computer

Computer type
PC/Desktop
OS
64 bit Windows 7
Hi Winspear1,

Within the command line, % symbols are strictly reserved to detonate variables and there is no way to configure CMD to interpret a line in the form of " %folder script ".

And I'm not certain you have gotten the whole concept of how paths work on the command line and how it affects when scripts may be run. So get comfy in your seat for a bit of theory...

When a script name is entered into the command line, it begins looking for the script in the Current Working Directory. The CWD is indicated at the prompt.

2cgbm0m.jpg


In the above example, "C:\Users\Admin" is the current working directory. When CMD starts, C:\Users\%USERNAME% is the default CWD (usually).

If the script is not found there, CMD will then go on a hunt looking for the script in specific predefined directories. These directory locations are defined in the command line variable: %PATH%. (Use the command: echo %PATH% to view them all)

And only then, if the script is not found in those locations, the shell will return an error similar to this:

2cngwmu.jpg




So to run your script, you've got about 2 options.

Option 1

Change your current working directory each time before running the script (as most will do). You can do this with the "cd" command.
ie:
Code:
cd "C:\Users\Name\pythonFiles"
somescript
( Or simply
Code:
cd "pythonFiles"
somescript
(^^Because 'pythonFiles' is a child directory of the CWD) )


Note: '.py' at the end of the script name is not required (CMD understands that it is a Python script; no need to tell it that ;) ).

Note 2: Nor is the location of the Python interpreter needed.
ie:
Code:
"C:\Python34\Python.exe" "C:\Users\Name\pythonFiles\somescript"
(Ditto reasoning)


But I assume you do not want to go with this option as you intend to do as limited typing as possible to invoke your script. So I present to you option 2:

Option 2

Place the script into a location that is predefined by the %PATH% variable. But fret not; the path variable can be changed! A new directory location may be added to it.

Simply add "C:\Users\Name\pythonFiles" to the %PATH% variable and CMD will always look there for your script regardless of what the CWD is.

And believe me, this is exactly what you want. Because after that, all that needs to happen for you to be able to run your Python script is to type the name of your script into the command line.

5wa6xd.jpg


(Notice how the current working directory and script location differ)


I'm sure some nice person on here on this forum will have you a tutorial on how to edit the %PATH%.
 

My Computer

Computer type
PC/Desktop
OS
Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
You mean like an environment variable? If you type SET and press Enter in a command window you'll see all variables.

You can add new ones, example: set python=c:\somefolder
you use it for example as cd %python%
but the variable will only be available in that command windows session

If you want a variable to be globally you need to add it to your user variables:
Control Panel - System - Advanced System Settings - Environment variables(button in tab Advanced)
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
HP Elitebook 8540p
OS
Windows 7 Pro 32
CPU
Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
Motherboard
Hewlett-Packard 1521
Memory
4,00 GB (Usable 2,98)
Graphics Card(s)
NVIDIA NVS 5100M
Sound Card
NVIDIA High Definition Audio
Screen Resolution
1600x900
Hard Drives
INTEL SSDSA2CW120G3
Antivirus
F-Secure Internet Security
Browser
IE, Firefox, Opera
Other Info
Sandboxie,
SRP (Software Restriction Policy),
EMET (Enhanced Mitigation Experience Toolkit),
WFC (Windows Firewall Control by BiniSoft),
Malwarebytes Premium
Hi there,
Thank you for your long and extremely informative post! I hadn't even known about the cd command and have been typing out every directory in full, every time, what a lifesaver (or fingersaver anyway)

As to Option 2, that is exactly what I want to do, yes. However, I attempted to add to the path variable, and my command console doesn't seem to be registering it
My variable value now reads as follows: C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files\Broadcom\Broadcom 802.11 Network Adapter\Driver;;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;C:\Program Files\Common Files\Lenovo;C:\Program Files (x86)\Common Files\Ulead Systems\MPEG;C:\Program Files (x86)\Windows Live\Shared;C:\SWTOOLS\ReadyApps;C:\Program Files (x86)\Symantec\VIP Access Client\;C:\Program Files (x86)\Common Files\Lenovo;C:\Program Files\Lenovo Fingerprint Reader\;C:\Program Files\Lenovo Fingerprint Reader\x86\;C:\Users\Jamie\pythonFiles\
(Text small because mostly irrelevant). I'm not sure what I am doing wrong. The path i have listed is definitely correct because C:\Users\Jamie\pythonFiles\script1 runs the code correctly.
 

My Computer

Computer type
PC/Desktop
OS
64 bit Windows 7
in command prompt type:
Code:
path
Is correct folder listed at the end?
Is python.exe in that folder?
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
Close the command window and open a new one to get the updated PATH variable
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
HP Elitebook 8540p
OS
Windows 7 Pro 32
CPU
Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
Motherboard
Hewlett-Packard 1521
Memory
4,00 GB (Usable 2,98)
Graphics Card(s)
NVIDIA NVS 5100M
Sound Card
NVIDIA High Definition Audio
Screen Resolution
1600x900
Hard Drives
INTEL SSDSA2CW120G3
Antivirus
F-Secure Internet Security
Browser
IE, Firefox, Opera
Other Info
Sandboxie,
SRP (Software Restriction Policy),
EMET (Enhanced Mitigation Experience Toolkit),
WFC (Windows Firewall Control by BiniSoft),
Malwarebytes Premium
Yes, path was listing my file, and then i tested it and my script ran, so its updated correctly. Not sure why it didn't before, maybe I had the window open without pressing "ok" while i was trying to get it to work in the command prompt. Thanks so much for your help and tutorials everyone, I will mark this solved.
 

My Computer

Computer type
PC/Desktop
OS
64 bit Windows 7
Back
Top