Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Using PATHS in the command line in Windows 7

15 Oct 2014   #1
winspear1

64 bit Windows 7
 
 
Using PATHS in the command line in Windows 7

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 System SpecsSystem Spec
.
15 Oct 2014   #2
Kaktussoft

Microsoft Community Contributor Award Recipient

Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
 
 

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 System SpecsSystem Spec
15 Oct 2014   #3
Kaktussoft

Microsoft Community Contributor Award Recipient

Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
 
 

"c:\user\Jamie\<full path>\python" c:\whatever\subfolder\somescript.py

does work?
My System SpecsSystem Spec
.

15 Oct 2014   #4
winspear1

64 bit Windows 7
 
 

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 System SpecsSystem Spec
15 Oct 2014   #5
Kaktussoft

Microsoft Community Contributor Award Recipient

Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
 
 

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 System SpecsSystem Spec
15 Oct 2014   #6
winspear1

64 bit Windows 7
 
 

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 System SpecsSystem Spec
15 Oct 2014   #7
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

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.



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:





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.



(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 System SpecsSystem Spec
15 Oct 2014   #8
Tookeri

Windows 7 Pro 32
 
 

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 System SpecsSystem Spec
15 Oct 2014   #9
winspear1

64 bit Windows 7
 
 

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\Wind owsPowerShell\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 System SpecsSystem Spec
15 Oct 2014   #10
Kaktussoft

Microsoft Community Contributor Award Recipient

Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
 
 

in command prompt type:
Code:
path
Is correct folder listed at the end?
Is python.exe in that folder?
My System SpecsSystem Spec
Reply

 Using PATHS in the command line in Windows 7




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
Windows Management Instrumentation Command Line
I have never needed to use the Windows Management Instrumentation CLI before and i am unfamiliar with the syntax. Has anyone ever seen or written a tutoral for the WMI CLI before?
Performance & Maintenance
Windows Explorer Command Line
On one of my PCs running Win7 Pro 32 bit I'm getting odd behavior when I use command line switches for Windows Explorer. When I create a shortcut with the following command line it works perfectly everytime: C:\Windows\explorer.exe An normal Explorer window opens. If I create a shortcut...
General Discussion
Windows 7 x86 (Question about Rundll32 Command Line)
Hello, Today when I started my PC, as usual I always check Task Manager (processes). I saw a Rundll32.exe whitch is regular Windows process file (c:/windows/system32), then I checked Command Line of process and it says this: C:\Windows\System32\rundll32.exe...
General Discussion
Windows Backup command line
Hi all, i'm very happy to be here !!! :D I do not come often because I work a lot! But when I'm there, I am pleased. OK. I have a little problem... I'm looking for the command line of Windows backup (sdclt.exe). I went to the MSDN site and nothing about ! I found in google few commands but...
Backup and Restore
How To Back Up My Windows 7 Command Line
Dear sir how to backup my windows 7 command line script plz help me urgent back for local or remote location ....its possible in command line please help me.............
Backup and Restore
Windows Update by Command Line
In Windows 7, the normal way to start Windows Update is from Control Panel. However, is there a way to start Windows Update by command line instead, in Win 7? If someone knows, please put the exact command in your reply. Thanks in advance.
Windows Updates & Activation


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 00:26.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App