Using PATHS in the command line in Windows 7

Page 1 of 2 12 LastLast

  1. Posts : 5
    64 bit Windows 7
       #1

    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 Computer


  2. Posts : 10,796
    Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
       #2

    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


  3. Posts : 10,796
    Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
       #3

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

    does work?
      My Computer


  4. Posts : 5
    64 bit Windows 7
    Thread Starter
       #4

    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


  5. Posts : 10,796
    Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
       #5

    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


  6. Posts : 5
    64 bit Windows 7
    Thread Starter
       #6

    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


  7. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #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.



    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 Computer


  8. Posts : 1,049
    Windows 7 Pro 32
       #8

    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


  9. Posts : 5
    64 bit Windows 7
    Thread Starter
       #9

    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 Computer


  10. Posts : 10,796
    Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
       #10

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


 
Page 1 of 2 12 LastLast

  Related Discussions
Our Sites
Site Links
About 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 02:46.
Find Us