安装mysql connector后Python及Anaconda无法启动,导入错误求助
Hey Ric, let's get your Python and Anaconda back up and running. From the error log you shared, the core issue is that your system Python 3.6 is trying to load SQLAlchemy from your Anaconda package directory, which causes a relative import failure—SQLAlchemy's mysql/types.py uses relative imports that don't work when loaded outside the Anaconda environment context.
Here's how to fix this step by step:
1. Fix the conflicting site-packages path
First, we need to stop system Python from accessing Anaconda's packages:
- Navigate to your system Python's site-packages folder:
C:\Users\ric22\AppData\Local\Programs\Python\Python36\lib\site-packages - Look for
.pthfiles (especiallyprotobuf-3.5.2.post1-py3.6-nspkg.pthmentioned in the error) and open them with a text editor. - If any line in these files points to your Anaconda path (like
D:\Programme\Anaconda\pkgs\sqlalchemy-1.1.13-py36h5948d12_0\Lib\site-packages), delete that line. Alternatively, rename the.pthfile to something likeprotobuf-3.5.2.post1-py3.6-nspkg.pth.bakto temporarily disable it.
2. Separate your system Python and Anaconda environments
Conflicting PATH settings often cause this kind of cross-environment mess:
- Open your system's Environment Variables (search for "Edit the system environment variables" in Windows search).
- In the
PATHvariable, check if Anaconda's directories are listed before your system Python 3.6 directories. If so, move the system Python paths to the top, or temporarily remove the Anaconda paths to test if Python starts correctly. - For Anaconda usage, always use
conda activateto launch the specific environment you need instead of runningpython.exedirectly from the command prompt.
3. Uninstall and reinstall the problematic package
If the path fixes don't work, let's reset the MySQL Connector installation:
- Open a command prompt and run:
(If you installed a different variant, usepip uninstall mysql-connector-pythonmysql-connectorinstead ofmysql-connector-python.) - Next, repair your system Python's core packages with:
python -m ensurepip --upgrade - When you need to reinstall MySQL Connector later, make sure you're installing it in the correct environment:
- For system Python: Use
pip install mysql-connector-python - For Anaconda: Activate your environment first, then run
conda install mysql-connector-python
- For system Python: Use
4. Verify the fix
After completing the steps above:
- Run
python.exein the command prompt to check if the interactive shell starts normally. - Test launching IDLE with
python -m idlelib.idleto confirm it works too.
内容的提问来源于stack exchange,提问作者RicRoc

