Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: Python scripts not working

  1. #1

    Default Python scripts not working

    Hi
    Recently i have been learning python and yesterday i run a script that did not work. Since then a lot of the scripts i have made do not work
    I have noticed that when i do a 'LS' in the directory that contains the scripts all the scripts that don't work the text is coloured black and the ones
    that do work are coloured green. When i run the script i got this output


    Code:
    /path to the script including /name,py  $ Line 1 \r command not found
    This was the case for about eight lines then the script stopped. Line 1 is just some commented out text eg name of script

    I am using opensuse 13.1 and the latest version of python

    Thanks

  2. #2
    Join Date
    Nov 2013
    Location
    Canadiana
    Posts
    461

    Default Re: Python scripts not working

    On 05/01/2014 11:36 PM, Subzero01 wrote:
    >
    > Hi
    > Recently i have been learning python and yesterday i run a script that
    > did not work. Since then a lot of the scripts i have made do not work
    > I have noticed that when i do a 'LS' in the directory that contains
    > the scripts all the scripts that don't work the text is coloured black
    > and the ones
    > that do work are coloured green. When i run the script i got this
    > output
    >
    >
    >
    > Code:
    > --------------------
    > /path to the script including /name,py $ Line 1 \r command not found
    > --------------------
    >
    >
    > This was the case for about eight lines then the script stopped. Line 1
    > is just some commented out text eg name of script
    >
    > I am using opensuse 13.1 and the latest version of python
    >
    > Thanks
    >
    >


    Just a wild guess but do the scripts have permission to actually
    execute? I am only asking this since you mentioned the ones that work
    are green for ls. You can check the permissions using ls -l.

    Read more about it here
    http://www.linux.com/learn/tutorials...le-permissions

    --
    Bring the Penguins Back! https://features.opensuse.org/316767
    openSUSE 13.1
    KDE 4.13.0

  3. #3

    Default Re: Python scripts not working

    Quote Originally Posted by Subzero01 View Post
    Hi
    Recently i have been learning python and yesterday i run a script that did not work. Since then a lot of the scripts i have made do not work
    I have noticed that when i do a 'LS' in the directory that contains the scripts all the scripts that don't work the text is coloured black and the ones
    that do work are coloured green. When i run the script i got this output


    Code:
    /path to the script including /name,py  $ Line 1 \r command not found
    This was the case for about eight lines then the script stopped. Line 1 is just some commented out text eg name of script

    I am using opensuse 13.1 and the latest version of python

    Thanks
    That \r is a Windows line endings aka Carriage return, did you created that scrip from a Windows text editor or from a Windows machine? If so you should know that Unixes and Windows has different line endings. Unix systems use Line Feeds (LFs) only. You can check it out

    Code:
    cat -e name.py
    or

    Code:
    sed -n l name.py

    If you see something like

    Code:
    Blahblahblah\r$
    \r$
    More Blah blah blah\r$
    Then you probably have Cr in your scripts. Some windows text editor you can configure the line endings and choose which one you want but don't ask me how to do it in Windows . Any way it is just a hunch but please do send us some more info.
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  4. #4

    Default Re: Python scripts not working

    Quote Originally Posted by alanbortu View Post
    On 05/01/2014 11:36 PM, Subzero01 wrote:
    >
    > Hi
    > Recently i have been learning python and yesterday i run a script that
    > did not work. Since then a lot of the scripts i have made do not work
    > I have noticed that when i do a 'LS' in the directory that contains
    > the scripts all the scripts that don't work the text is coloured black
    > and the ones
    > that do work are coloured green. When i run the script i got this
    > output
    >
    >
    >
    > Code:
    > --------------------
    > /path to the script including /name,py $ Line 1 \r command not found
    > --------------------
    >
    >
    > This was the case for about eight lines then the script stopped. Line 1
    > is just some commented out text eg name of script
    >
    > I am using opensuse 13.1 and the latest version of python
    >
    > Thanks
    >
    >


    Just a wild guess but do the scripts have permission to actually
    execute? I am only asking this since you mentioned the ones that work
    are green for ls. You can check the permissions using ls -l.

    Read more about it here
    http://www.linux.com/learn/tutorials...le-permissions

    --
    Bring the Penguins Back! https://features.opensuse.org/316767
    openSUSE 13.1
    KDE 4.13.0
    If it is a permission issue the error would probably be

    Code:
    name.py: Permission denied
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  5. #5

    Default Re: Python scripts not working

    Quote Originally Posted by Subzero01 View Post
    Hi

    Code:
    /path to the script including /name,py  $ Line 1 \r command not found
    This was the case for about eight lines then the script stopped. Line 1 is just some commented out text eg name of script

    Thanks
    The very first line of your script should be the shebang or the hashbang and the first two bytes should be #! followed by the absolute path where your executable resides, e.g /bin/python and it is a MUST for the kernel to know how to interpret your script.
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  6. #6

    Default Re: Python scripts not working

    On 2014-05-02, Subzero01 <Subzero01@no-mx.forums.opensuse.org> wrote:
    >
    > Hi
    > Recently i have been learning python and yesterday i run a script that
    > did not work.


    It would also helpful if you could provide example of one of your scripts which do not work.

    > Since then a lot of the scripts i have made do not work
    > I have noticed that when i do a 'LS' in the directory that contains
    > the scripts all the scripts that don't work the text is coloured black
    > and the ones
    > that do work are coloured green. When i run the script i got this
    > output
    >
    >
    >
    > Code:
    > --------------------
    > /path to the script including /name,py $ Line 1 \r command not found
    > --------------------


    Please clarify: are you naming your scripts `name,py' (with a comma) or `name.py' (with a full stop) - you should use a
    full stop, not a comma before an extension.

    > I am using opensuse 13.1 and the latest version of python


    Please confirm then you are using Python 3.x rather than Python: 2.7.x.

  7. #7
    Join Date
    Nov 2013
    Location
    Canadiana
    Posts
    461

    Default Re: Python scripts not working

    Quote Originally Posted by jetchisel View Post
    If it is a permission issue the error would probably be

    Code:
    name.py: Permission denied

    Yeah I know but OP mentioned green so I figured I would ask that.

    And I checked after I made the post and you can run the script without execute permission with this (you dont even need #!/path/to/python)
    Code:
    python file.py
    Cant edit those posts though

    Regarding the \r, isnt there some utility that can do the conversion for you? I think its called dos2unix.

    And to the poster above me, even if the file is named name,py, linux doesnt seem to care (try it). I think some clarification is needed, maybe the source code if OP is willing.
    openSUSE Leap (42.1) 64 bit
    Plasma 5.4

  8. #8

    Default Re: Python scripts not working

    On 2014-05-02, alanbortu <alanbortu@no-mx.forums.opensuse.org> wrote:
    > And to the poster above me, even if the file is named name,py, if OP is
    > running with the command I posted above, linux doesnt seem to care. I
    > think some clarification is needed.


    Perhaps I'm misunderstanding, but GNU/Linux (or rather Python running within GNU/Linux) does care if you're trying
    `python name,py' when the file is called `name.py' or the other way round. But I certainly agree with you that much more
    clarification is required from the poster. It should always be possible to execute a python script within a Python
    shell e.g.:

    Code:
    sh-4.2$ python
    Python 2.7.6 (default, Nov 21 2013, 15:55:38) [GCC] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> execfile("name.py")
    If that doesn't work, then there are only three possibilities:

    1. The script isn't in an available Python path or current working directory.
    2. The code has an error.
    3. There's something wrong with the Python installation or site packages.

    Whether or not `name.py' is executable or not is irrelevant because it is a source file not an executable binary.


  9. #9
    Join Date
    Nov 2013
    Location
    Canadiana
    Posts
    461

    Default Re: Python scripts not working

    Sorry, this post is kinda long.

    Quote Originally Posted by flymail View Post
    On 2014-05-02, alanbortu <alanbortu@no-mx.forums.opensuse.org> wrote:
    > And to the poster above me, even if the file is named name,py, if OP is
    > running with the command I posted above, linux doesnt seem to care. I
    > think some clarification is needed.


    Perhaps I'm misunderstanding, but GNU/Linux (or rather Python running within GNU/Linux) does care if you're trying
    `python name,py' when the file is called `name.py' or the other way round.
    Ah maybe, I was just talking about something like this:
    Code:
    > cat test,py 
    print("hi")
    
    >python test,py 
    hi
    But when I tried as you suggested:
    Code:
    > ls
    test,py
    
    > python2
    Python 2.7.6 (default, Nov 21 2013, 15:55:38) [GCC] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> execfile("test,py")
    hi
    >>>
    Quote Originally Posted by flymail View Post
    Whether or not `name.py' is executable or not is irrelevant because it is a source file not an executable binary.
    Really? Because when I try to run python files from zsh I get permission errors. I am guessing you are talking about the python shell?
    Code:
    > ll test.py 
    -rw-r--r-- 1 gum users 31 May  2 10:44 test.py
    
    > cat test.py 
    #!/usr/bin/python3
    print("hi")
    
    > ./test.py
    zsh: permission denied: ./test.py
    ./test.py: command not found         
    
    > chmod u+x test.py
    > ./test.py 
    hi
    openSUSE Leap (42.1) 64 bit
    Plasma 5.4

  10. #10
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    25,245

    Default Re: Python scripts not working

    There seem to be some misunderstrandings here. My contribution for what it is worth.

    A program (any program, binary or script) can only be executed directly (e.g. by using it as first word in a command line) when the user doing the call has execute rights for the file. Thus in:
    Code:
    henk@boven:~/bin> l
    totaal 36
    drwxr-xr-x  2 henk wij  4096  2 mei 17:29 ./
    drwxr-xr-x 84 henk wij  4096  2 mei 15:25 ../
    -rwxr--r--  1 henk wij   434 23 jul  2012 fototime*
    -rwxr-xr-x  1 henk wij 19126 23 jan  2011 mmcheck*
    -rw-r--r--  1 henk wij   434  2 mei 17:29 somescript
    henk@boven:~/bin>
    the file forotime can be executed by the owner (henk) and by nobody else. The file mmcheck can be executed by anybody (if having access to the directory of course). The file somescript can not be executed:
    Code:
    henk@boven:~/bin> somescript
    bash: /home/henk/bin/somescript: Toegang geweigerd
    henk@boven:~/bin>
    (meaning Permission denied).
    The * is an extra indication that the execute permission is set and in some terminal emulations, the filename is also shown in green.

    The first word on a command line (the program that should be executed) is in fact a file name. That file must of course be found. That can either be done by giving it a path (either relative or absolute). Many use, during testing, the relative path ./ when their working directory is the directory that contains the file. Another much used option is having the file in one of the directories mentioned in the PATH environment variable. For that reason it is often nice to put a (production) program in ~/bin. You can then call it without any path added.

    When such a program is a script, the kernel must know which interpreter to use to run it. In fact then not the file is loaded, but the interpreter and the file is offered as an argument to the interpreter. Telling the kernel is done using the "shebang". The first line with, in the case of pyton:
    Code:
    #!/usr/bin/python3
    (I saw this above, I do not know if this is the correct call, but I hope you get the message).

    There is allways the possibility to ignore all these niceties and call the interpreter yourself and giving the script file as an argument.

    All this is regardless of the suffix of the file name. When you like it to have the suffix of .py, that is of course OK, but it is purely for your own administration and/or satisfaction.. Unix/Linux has no concept of an "Extension" as e.g. MS-DOS has.

    My two cents.
    Henk van Velden

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •