简体   繁体   English

在Windows 8.1上使用Django的manage.py runserver出错

[英]Error in manage.py runserver with Django on windows 8.1

I couldn't find this exitcode anywhere but hopefully one of you could help me or let me know if this is a bug in python/Django. 我无法在任何地方找到这个exitcode,但希望你们中的一个可以帮助我或让我知道这是否是python / Django中的错误。

Anyway, first here's the stacktrace: 无论如何,首先是这里的堆栈跟踪:

    Traceback (most recent call last):
  File "C:\Sitezooi\SiteTest\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Program Files\Python\lib\site-packages\django\core\management\__init_
_.py", line 385, in execute_from_command_line
    utility.execute()
  File "C:\Program Files\Python\lib\site-packages\django\core\management\__init_
_.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Program Files\Python\lib\site-packages\django\core\management\base.py
", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Program Files\Python\lib\site-packages\django\core\management\base.py
", line 338, in execute
    output = self.handle(*args, **options)
  File "C:\Program Files\Python\lib\site-packages\django\core\management\command
s\runserver.py", line 83, in handle
    self.run(*args, **options)
  File "C:\Program Files\Python\lib\site-packages\django\core\management\command
s\runserver.py", line 92, in run
    autoreload.main(self.inner_run, args, options)
  File "C:\Program Files\Python\lib\site-packages\django\utils\autoreload.py", l
ine 322, in main
    reloader(wrapped_main_func, args, kwargs)
  File "C:\Program Files\Python\lib\site-packages\django\utils\autoreload.py", l
ine 293, in python_reloader
    exit_code = restart_with_reloader()
  File "C:\Program Files\Python\lib\site-packages\django\utils\autoreload.py", l
ine 279, 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: inval
id character

Ignore the weird filepath please, I even just tried putting it on C:\\ directly. 请忽略这个奇怪的文件路径,我甚至只是尝试将它直接放在C:\\上。

There's another maybe similar Stackoverflow Question here: UnicodeEncodeError when using the compile function but it's not like the filepath I use uses any non-English characters. 这里有另一个类似的Stackoverflow问题: 使用编译函数时的UnicodeEncodeError,但它不像我使用的文件路径使用任何非英文字符。 I tried a couple solutions there but they didn't work. 我在那里尝试了几种解决方案,但它们没有用。

Running python 3.4.1, tested in 2.7.x before, didn't work either. 运行python 3.4.1,之前在2.7.x中测试过,也没有用。 Runs fine on linux(Ubuntu). 在linux(Ubuntu)上运行良好。

There's nothing special in the django project since it's just the empty startproject project. django项目没有什么特别之处,因为它只是空的startproject项目。

I was having the same issue, and I found the solution. 我遇到了同样的问题,我找到了解决方案。 From what I searched it also happens with Windows 7 & 8. 从我搜索到的内容也发生在Windows 7和8中。

If you want to know with more detail how I solved it check the ticket I filed in Django's forums: Error in manage.py runserver on Windows (7 / 8 / 8.1) . 如果您想更详细地了解我是如何解决的,请查看我在Django论坛中提交的票证: Windows上的manage.py runserver出错(7/8 / 8.1)

Now to solve the error open this file C:\\Program Files\\Python\\lib\\site-packages\\django\\utils\\autoreload.py (I'm using your code as reference) and add this line of code just before your error (line 279): 现在解决错误打开这个文件C:\\ Program Files \\ Python \\ lib \\ site-packages \\ django \\ utils \\ autoreload.py (我使用你的代码作为参考)并在你的错误之前添加这行代码(第279行):

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

Your function now should look like this: 你现在的功能应该是这样的:

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'
        new_environ['PATH'] = os.path.abspath(new_environ['PATH'].replace('\u202a', ''))
        exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)
        if exit_code != 3:
            return exit_code

Now try using again manage.py runserver. 现在再次尝试使用manage.py runserver。 I hope this solves your problem and don't feel you're alone. 我希望这能解决你的问题,不要觉得你是孤身一人。

In my case it had nothing to do with PATH , there seem to be CHROME_RESTART environment setting with some non-english characters. 在我的情况下,它与PATH无关,似乎有一些非英文字符的CHROME_RESTART环境设置。 Poping it from new_environ did the trick: new_environ弹出它就可以了:

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'

        # This will prevent UnicodeEncodeError
        new_environ.pop("CHROME_RESTART", None)

        exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)
        if exit_code != 3:
            return exit_code

I tried this 我试过这个

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

but it did not work. 但它不起作用。

And my solution is 我的解决方案是

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

Hope it will help you! 希望它能帮到你!

UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1:invalid character

I had the same problem on windows 7 with 我在Windows 7上遇到了同样的问题

$ python manage.py runserver

Just in case if someone has cyrillic computer name like I had, it's exactly the thing causing your encoding problem. 万一有人像我一样拥有西里尔语的计算机名称,这正是造成编码问题的原因。 So the solution is to rename your computer using latin alphabet symbols only. 所以解决方案是仅使用拉丁字母符号重命名您的计算机。

I had the same problem. 我有同样的问题。
The reason was non latin characters in an environment variables entry. 原因是环境变量条目中的非拉丁字符。
In my case it was cyrilic name of some folder, while my windows was originally english version. 在我的情况下,它是一些文件夹的cyrilic名称,而我的Windows最初是英文版。
So it had a conflict. 所以它有冲突。 After removing it - everything worked ok. 删除后 - 一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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