0

I am trying to run link to my script from command line i do this like this:

python "script.lnk" 

In the script.py file i added this line as a first line in file:

# coding=UTF-8 

I am positive that my Notepad++ has encoding set to UTF-8 and still i get this error:

SyntaxError: Non-ASCII character '\xd1' in file script.lnk on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details 

Any ideas why?

5
  • hello, is this script.lnk or script.py? Is the lnk file some kind of link files? If so, it may be binary, double-clicking on it may work but the file system may not see it as a symbolic link. What happens if you use python "script.py" directly where your script is located?
    – birdypme
    CommentedOct 23, 2015 at 12:58
  • When i run "python script.py" everything works ok. I want to put lnk's to all my scripts in one directory so I wouldn't have to navigate between different directories to run them. When i run "python 'script.lnk' i get this error. The .lnk file was created via right_mouse_button on .py file -> send to desktop (create shortcut).
    – Zwierzak
    CommentedOct 23, 2015 at 13:13
  • I think what you need is not a 'shortcut', which is only understood by explorer.exe, but a 'symbolic link'. howtogeek.com/howto/16226/…
    – birdypme
    CommentedOct 23, 2015 at 13:30
  • Thanks @birdypme it helped a lot! Exactly what I was looking for.
    – Zwierzak
    CommentedOct 23, 2015 at 14:39
  • You're welcome, I'm glad I could help. I think you should accept anti1869's solution, as he answered your question "Any ideas why?".
    – birdypme
    CommentedOct 26, 2015 at 9:45

2 Answers 2

1

Pay attention to error message, it says error is in your .lnk file.

"...character '\xd1' in file script.lnk on line 2..."

By executing python "script.lnk" you are trying to feed python interpreter with shortcut file, not the actual python script.

I assume you're on Windows, so if you need to make shortcut to your python script, you must make shortcut to python interpreter executable instead with script location as a parameter.

2
  • What I want to achieve is to be able to run my programs (that are folder-separated) without the need to change directories in command line. I was thinking about putting all main.py shortcuts in one place and running shortcuts. Any better solution? Yours would work but it's not elegant and takes time to create python interpreter shortcuts with parameter ;P
    – Zwierzak
    CommentedOct 23, 2015 at 13:30
  • Probably, batch files will help in your case. Sorry, I'm not windows user to have more details on that. Idea is to make batch that will change working dir into dir with your script and the run it. Try to look here steve-jansen.github.io/guides/windows-batch-scripting/…
    – anti1869
    CommentedOct 23, 2015 at 13:38
0

Add this as first or second line:

# -*- coding: utf-8 -*- 
0

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.