Blanen Blanen - 2 days ago 5
Python Question

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.

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.

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. 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.
Runs fine on linux(Ubuntu).

There's nothing special in the django project since it's just the empty startproject project.

Answer

I was having the same issue, and I found the solution. From what I searched it also happens with 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).

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):

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. I hope this solves your problem and don't feel you're alone.

Comments