首頁  >  問答  >  主體

python - 刚创建项目 django manage.py runserver 报错UnicodeEncodeError:'mbcs'

win10系统,python 3.5.2, djang 1.10.3
刚刚创建django项目

C:\Users\JD\Desktop\firstsite>python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python35\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Python35\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python35\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python35\lib\site-packages\django\core\management\commands\runserver.py", line 58, in execute
    super(Command, self).execute(*args, **options)
  File "C:\Python35\lib\site-packages\django\core\management\base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:\Python35\lib\site-packages\django\core\management\commands\runserver.py", line 97, in handle
    self.run(**options)
  File "C:\Python35\lib\site-packages\django\core\management\commands\runserver.py", line 106, in run
    autoreload.main(self.inner_run, None, options)
  File "C:\Python35\lib\site-packages\django\utils\autoreload.py", line 334, in main
    reloader(wrapped_main_func, args, kwargs)
  File "C:\Python35\lib\site-packages\django\utils\autoreload.py", line 305, in python_reloader
    exit_code = restart_with_reloader()
  File "C:\Python35\lib\site-packages\django\utils\autoreload.py", line 291, in restart_with_reloader
    exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)
UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character

网上没有搜出成功又可解决我的问题的办法。
但有别人成功却不能解决我的:

http://stackoverflow.com/ques... 或者 https://segmentfault.com/q/10...
在~Python35\lib\site-packages\django\utils\autoreload.py中添加

new_environ['PATH']=os.path.abspath(new_environ['PATH'].replace('\u202a', ''))

也可尝试添加

new_environ['PATH'] = os.path.abspath(new_environ['PATH'].encode('ascii', 'replace'))

但这两种方法都不能解决的问题。

希望有经验的同学能够看到我的问题并好心解答
谢谢

迷茫迷茫2764 天前1132

全部回覆(1)我來回復

  • 大家讲道理

    大家讲道理2017-04-18 09:56:03

    我自己解決了呃。
    主要還是去C:Python35Libsite-packagesdjangoutils裡面找autoreload.py這個檔案的問題。
    定位:autoreload.py --- 函數restart_with_reloader。最終採用的程式碼如下:

    def restart_with_reloader():
        while True:
            args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions] + sys.argv
            if sys.platform == "win32":
                args = ['"%s"' % arg for arg in args]
            new_environ = os.environ.copy()
            new_environ["RUN_MAIN"] = 'true'
            # 删除CLASSPATH
            if 'CLASSPATH' in new_environ.keys():
                del new_environ['CLASSPATH']
            exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)
            if exit_code != 3:
                return exit_code
                

    看起來有點亂,但意思其實就是一個一個印出來看哪個有問題。
    我發現在我的new_environ裡面,有個CLASSPATH字段出了問題,

    'CLASSPATH': '.;\u202aC:\Program Files\Java\jdk1.8.0_101\bin'
    

    於是我就想辦法把這裡面的U202a去掉,後來去不掉(因為很神奇地,單獨定位print(new_environ['CLASSPATH'])是沒有u202a的,靠),我乾脆del刪除掉。
    嗯,IT WORKED

    回覆
    0
  • 取消回覆