1

I am a user of python under mac and need to work now on a windows system. I have installed Python35 for windows in Powershell, the command py --version and python --version provides me "Python 3.5.2".

I want to run a python script in Powershell and have tried : py file.py python file.py py .\file.py python .\file.py and have the following :

python :   File ".\file.py", line 1
Au caractère Ligne:1 : 1
+ python .\file.py
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (  File ".\file.py", line 1:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

SyntaxError: Non-UTF-8 code starting with '\xff' in file .\file.py on line 1, but no encoding declared; see 
http://python.org/dev/peps/pep-0263/ for details

As for .\file.py it opens me a black window that closes within 1 second.

Interestingly, when doing python or py it outputs the python idle but powershell is blocked and I cannot use it either in powershell.

Now, I already looked at

cannot run python script file using windows prompt

Cannot run python script

Run python script inside powershell script

As well as I tried to add the Python Path in Powershell and manully in the "Advance system" interface of windows.

By the way my file.py contains a simple print("hello")

What am I doing wrong?

Community
  • 1
  • 1
tuttifolies
  • 1,417
  • 2
  • 14
  • 20
  • Silly question, but in what editor did you edit or create your python script? This is what you get if you edit it in a non- plain text editor. – Jacob Vlijm Sep 06 '16 at 10:14
  • I used sublime text 3. And after "save with encoding" in UTF-8 it works now, as said in my answer to vonPryz. Do you think it is something coming from the sublime text editor? I often use it under mac for speed coding in several language, and did not encounter this issue until today. – tuttifolies Sep 06 '16 at 12:41
  • 1
    @JacobVlijm You have pointed me a good point. So in fact it does not come with the editor I was using. My problem was initially how I created my file. With mac I would have used : `echo "print('hello')" > file.py` then run `python3 file.py`. What happen is (being a newbie), I did in powershell `echo "print('hello')" >> file.py` which I guess have add an invisible character that it was complaining about. Creating a brand new file from the editor on the other hand worked perfectly fine in the end. – tuttifolies Sep 06 '16 at 12:52
  • Exactly! I knew because I once wrote a scrip (on AU) for an ex- mac user, mentioning he had the exact same issue. It took a while before we found out exactly what you mention :). – Jacob Vlijm Sep 06 '16 at 12:57

1 Answers1

0

The problem is explained in the error message:

SyntaxError: Non-UTF-8 code starting with '\xff' in file .\file.py on line 1, but no encoding declared;

It means that Python reads the source file and gets confused. On the other hand, it has a byte order mark header, but on the other hand there isn't enough information about what kind of Unicode the file is about.

It's better be safe than sorry, so Python requires you to tell what to do instead of trying to guess what's the real encoding. In unicode, you see, there are caveats. For example, ½ is actually a number that has value of 0.5.

As for how to solve this, either save the file as non-unicode (ANSI) or start the source file with, say, header:

# -*- coding: utf-8 -*-
vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • Thank you for your response. So unfortunately adding `# -*- coding: utf-8 -*-` did not work. I have try several comment coding at the start of the file as said in the link of the error message as you mentioned though still did not work. So I tried your solution of saving the file. So with the editor I "Saved with Encoding" and chose UTF-8. Then running either `python .\file.py` or `py .\file.py` finally worked. Thank you for vonPryz and sudomakeinstall2 – tuttifolies Sep 06 '16 at 12:36
  • 1
    Python doesn't support source files encoded as UTF-16, even with a coding spec. This PowerShell command creates a UTF-16 text file: `echo "print('hello')" >> file.py`. – Eryk Sun Sep 06 '16 at 16:59
  • For Python 3, a coding spec isn't required for UTF-8 because it's the assumed default. You only need this for UTF-8 if your text editor requires it. – Eryk Sun Sep 06 '16 at 17:11