如何解决PyCharm中Django运行时的OSError: [WinError 123]错误
Hey there! I see you're just getting started with Django and ran into this frustrating WinError 123 when trying to run your server. Let's break down what's going on and fix it, then get that Hello World page working.
First, let's recap the error you're seeing (formatted for clarity):
Traceback (most recent call last): File "manage.py", line 21, in <module> main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "C:\Users\W\PycharmProjects\manuproject\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line utility.execute() File "C:\Users\W\PycharmProjects\manuproject\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\W\PycharmProjects\manuproject\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv self.execute(*args, **cmd_options) File "C:\Users\W\AppData\Local\Programs\Python\Python37-32\lib\pathlib.py", line 1168, in stat return self._accessor.stat(self) OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '<frozen importlib._bootstrap>'
This error usually ties to path issues or version compatibility on Windows. Here are the most likely fixes to try:
Check for special characters in your project path
Windows is picky about paths with spaces, Chinese characters, or symbols like&,#, etc. Move your project to a simpler path likeC:\DjangoProjects\manuprojectand try running the server again.Fix Python-Django version compatibility
You're using Python 3.7-32, which doesn't play well with newer Django versions. Uninstall your current Django and install a compatible version:pip uninstall django pip install django==3.2.18Version 3.2.x is the last LTS release that fully supports Python 3.7.
Reset your virtual environment
Sometimes virtual environments get glitched out. Close PyCharm entirely, reopen it, reactivate your venv (runvenv\Scripts\activatein the terminal), then trypython manage.py runserveragain.
Once you've fixed the error, let's get that Hello World page up:
Create a new app (Django organizes code into apps)
python manage.py startapp helloRegister the app in settings
Open your project'ssettings.pyand add'hello'to theINSTALLED_APPSlist:INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'hello', # Add this line ]Write the view function
Openhello/views.pyand replace its content with:from django.http import HttpResponse def index(request): return HttpResponse("Hello World")Set up URLs for the app
Create a new filehello/urls.pyand add:from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]Link the app URLs to the project
Open your project's rooturls.pyand update it to:from django.contrib import admin from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), path('', include('hello.urls')), # Add this line ]Run the server again
python manage.py runserverNow visit
http://127.0.0.1:8000in your browser, and you should see your "Hello World" message!
内容的提问来源于stack exchange,提问作者Emmanuel Omulo

