2

I would like to run a Python script from a Hamilton C shell without having to specify the whole path of the script.

The first thing I tried was to add my scripts folder to the PATH environment variable in Windows. This worked, but the C shell cannot simply execute the .py files automatically, so I have to use "python" before the name of the script.

Now the problem is that since my scripts folder is not a Python path it is not recognized by the Python executable. So this does not work either.

I also tried to add my folder to the "PYTHONPATH" environment variable (which I had to create), but this does not work either.

Is there a way that I can run my script by either simply calling its name (myscript.py) or "python myscript.py"?

  • Any update on the below answers or do you find any worthy of acceptance or do you still have problems you need to resolve per this question of yours? – Pimp Juice IT Jan 13 '18 at 7:54
2

Run Python scripts from Windows command line with script name only

Is there any way that I can run my script by either simply calling its name (myscript.py) or "python myscript.py"?

Absolutely, you can do this in Windows with no problem. I tested this and confirmed it works as expected with Windows 10 as a matter of fact by following the below steps.


Configure Windows File Association Pointers

Set the python.exe full explicit path as in the below #3 for the correct value where this is on your system

  1. Open up an elevated [cmd.exe] command prompt as administrator
  2. Type in ASSOC .py=PythonScript and press Enter
  3. Type in FTYPE PythonScript="C:\Program Files\Python\python.exe" "%1" %* and press Enter
  4. Type in SET PATHEXT=.py;%PATHEXT% and press Enter

Confirm and Test Windows File Association Pointers

Now from a command prompt you can simply type in the full path to a python script and then press Enter, and the python logic should run accordingly.

You can optionally just type in the full path minus the .py file extension. You could also just CD to the directory where a python script is located, and then type in the script name (with or without the extension) and press Enter, and python should run the script logic accordingly.

Examples from CMD (post above changes)

  • "C:\ScriptPath\scriptname.py" and press Enter
  • "C:\ScriptPath\scriptname" and press Enter
  • CD /D "C:\ScriptPath\scriptname.py" and press Enter
  • CD /D "C:\ScriptPath\scriptname.py" and press Enter

Execute from Shell with Script Name only

If you really need to run the script from the command line without telling the shell the full explicit path to the python script file, then you need to add the path where this script resides to the %PATH% environmental variable.

  1. Open up an elevated [cmd.exe] command prompt as administrator
  2. Type in SET PATH=%PATH%;C:\Program Files\Python where the C:\Program Files\Python is the value where this exist on your system.

Now you can just type in the script name with or without the file extension without doing a CD to another directory or explicitly specifying the full path to the python script.


Further Resources

  • Windows Environment Variables

  • FTYPE /?

    Displays or modifies file types used in file extension associations
    
    FTYPE [fileType[=[openCommandString]]]
    
      fileType  Specifies the file type to examine or change
      openCommandString Specifies the open command to use when launching files
                        of this type.
    
    Type FTYPE without parameters to display the current file types that
    have open command strings defined.  FTYPE is invoked with just a file
    type, it displays the current open command string for that file type.
    Specify nothing for the open command string and the FTYPE command will
    delete the open command string for the file type.  Within an open
    command string %0 or %1 are substituted with the file name being
    launched through the assocation.  %* gets all the parameters and %2
    gets the 1st parameter, %3 the second, etc.  %~n gets all the remaining
    parameters starting with the nth parameter, where n may be between 2 and 9,
    inclusive.  For example:
    
        ASSOC .pl=PerlScript
        FTYPE PerlScript=perl.exe %1 %*
    
    would allow you to invoke a Perl script as follows:
    
        script.pl 1 2 3
    
    If you want to eliminate the need to type the extensions, then do the
    following:
    
        set PATHEXT=.pl;%PATHEXT%
    
    and the script could be invoked as follows:
    
        script 1 2 3
    
  • ASSOC /?

    Displays or modifies file extension associations
    
    ASSOC [.ext[=[fileType]]]
    
      .ext      Specifies the file extension to associate the file type with
      fileType  Specifies the file type to associate with the file extension
    
    Type ASSOC without parameters to display the current file associations.
    If ASSOC is invoked with just a file extension, it displays the current
    file association for that file extension.  Specify nothing for the file
    type and the command will delete the association for the file extension.
    
1

I'm the author of Hamilton C shell. It sounds like you're using Cygwin with my C shell and getting the following error when you try to use python:

1 C% python
csh(C:\cygwin64\bin\python line 1):  Couldn't open 'symlink' as a redirected standard input.
> in C:\cygwin64\bin\python
< called from line 1

The problem is that the Cygwin python command is a Cygwin-only symbolic link file rather than an actual executable. They're only supported by Cygwin. (You may have noticed that cmd.exe won't run it either.) Here's what's in it:

2 C% whereis python
C:\cygwin64\bin\python
3 C% cat `!!`
cat `whereis python`
!<symlink>python2.7.exe

Not being able to recognize it as anything else, the C shell tried to interpret it as a script, thought it recognized a !< C shell i/o redirection operator but could not find a file named symlink, hence the error message.

But given that all that file is doing is redirecting you to the actual executable, you can do the same thing with a C shell alias which you can save in your startup.csh file:

4 C% alias python python2.7
5 C% python
Python 2.7.10 (default, Jun  1 2015, 18:05:38)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
6 C% cd desktop
7 C% cat MyScript.py
print("Hello")
8 C% python MyScript.py
Hello

If you'd like to run your python scripts directly from the C shell without having to type the python command, the C shell supports the common #! syntax to tell it to use the python interpreter. But notice it still needs the name of the actual executable. Here's an example:

9 C% cat MyScript2.py
#!python2.7
print("Hello")
10 C% MyScript2.py
Hello
11 C%
0

Say you have a Python script called MyScript.py under a given directory:

print("Hello")

You could find out where Python is on your system:

$ which python /usr/bin/python

Adding the following shebang at the beginning of MyScript.py, your script will easily find the Python interpreter, and you will be able to execute it without invoking Python first:

#!/usr/bin/python
print("Hello")

$ ./MyScript.py Hello

(At this point you may also want to drop the .py extension.)

Should you not be under the same directory that contains MyScript.py, you will need to tell the shell where to find it. This can be done by adding the path to your PATH environment variable as follows:

$ setenv PATH <path to my script>:$PATH

or

$ setenv PATH $PATH:<path to my script>

Now MyScript.py can be executed from anywhere.

If you want to make this permanent you could add this line to your .cshrc, .login or similar file.

  • This seems to apply mostly to Linux, but I am on Windows so I don't think this works. I already added my folder to the PATH variable and it works (meaning I can call my script from another folder). The problem now is that calling the script doesn't execute it with the python interpreter and calling it with the python interpreter fails to find the script. – Alexandre Vanier Aug 15 '16 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.