简体   繁体   中英

SyntaxError in python.exe on PyCharm 5.0.4

I am running a python web application in PyCharm 5.0.4, I got an error:

C:\Users\xxx\AppData\Local\Programs\Python\Python35-32\python.exe C:\Users\xxx\AppData\Local\Programs\Python\Python35-32\python.exe C:/Users/xxx/PycharmProjects/xxxxx/run.py
File "C:\Users\pli\AppData\Local\Programs\Python\Python35-32\python.exe", line 1
SyntaxError: Non-UTF-8 code starting with '\x90' in file C:\Users\pli\AppData\Local\Programs\Python\Python35-32\python.exe on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Process finished with exit code 1

My python version is 3.5.1. But I can run the python code from CMD. I am thinking whether it is because of the configuration as there shouldn't be such error in python.exe.

配置 https://s14.postimg.org/ren7faib5/Screenshot_2016_02_27_13_06_01.png

The code is common Flask web application code:

# -*- coding: utf-8 -*-
import datetime

from flask import g, session, request, make_response
from flask_sqlalchemy import SQLAlchemy
from app.users.models import User, UserSession
from app import app, db

@app.before_request
def before_request():
    g.user = None
    if session.get('user_id'):
        user = User.query.filter_by(id=session.get('user_id')).first()
        g.user = user
....

The problem is because "Interpreter options" on Run/Debug Configuration. I found what in "Interpreter options" is "C:\\Users\\xxx\\AppData\\Local\\Programs\\Python\\Python35-32\\python.exe". But this is not legal command.

So to solve this problem, I open "Tool" --> "Run/Debug Configuration" --> set "Interpreter options" to empty, everything works.

在此处输入图片说明

So, for those who don't understand Chinese, (a since deleted answer) suggested to define source code encodings for the file.

If you read the error carefully enough, PyCharm tells you everything you need to solve this problem:

SyntaxError: Non-UTF-8 code starting with '\\x90' in file C:\\Users\\pli\\AppData\\Local\\Programs\\Python\\Python35-32\\python.exe on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Now, as stated in PEP 0263 :

Python will default to ASCII as standard encoding if no other encoding hints are given ... To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file.

Have you tried sticking a magic comment dictating the source code like:

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

...

I recall running into the same problem before and defining the source code encoding immediately fixed the problem for me.

I had a similar issue. But in my case by mistake, I selected Python.exe path in place of "script path" also.

After seeing the above screenshot I understood the script path should be the current script we are running. Fixed this and it worked. enter image description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM