Python新手求助:已安装bitmex-ws仍提示No module named 'bitmex_websocket'
I’ve run into this exact issue when working with BitMEX’s Python websocket tools, so let’s walk through the most likely fixes to get you up and running:
1. Fix the Virtual Environment/Interpreter Mismatch
This is by far the most common cause of this error. PyCharm might be using a different Python environment than the one you installed bitmex-ws into. Here’s how to check and fix it:
- Open PyCharm, go to File > Settings > Project: [Your Project Name] > Python Interpreter
- Copy the interpreter path shown at the top (it might look like
C:\Users\Ben\AppData\Local\Programs\Python\Python311\python.exeor a path to a virtual environment) - Open your command prompt/terminal and run this command to check if
bitmex-wsis installed in that environment:[copied-interpreter-path] -m pip list - If you don’t see
bitmex-wsin the list, install it directly to this interpreter with:[copied-interpreter-path] -m pip install bitmex-ws
2. Verify the Package Installed Correctly
Sometimes installations fail silently, leaving missing files. Let’s confirm the package exists in your site-packages:
- Run this command in your terminal to find your Python site-packages folder:
python -c "import site; print(site.getsitepackages())" - Navigate to that folder and check for a subfolder named
bitmex_websocket. If it’s missing, reinstall the package with verbose output to catch errors:pip install --force-reinstall -v bitmex-ws
3. Test Outside PyCharm to Rule Out IDE Issues
To make sure the problem isn’t specific to PyCharm:
- Create a simple test file (e.g.,
test_import.py) with just these lines:from bitmex_websocket import BitMEXWebsocket print("Import successful!") - Run it directly from your terminal using the same Python interpreter you installed the package to:
python test_import.py - If this works, go back to step 1 to fix PyCharm’s interpreter configuration.
4. Critical: You Might Be Using the Wrong Package
Wait a second — the official BitMEX Python Websocket connector from their GitHub repo isn’t published on PyPI. The bitmex-ws package you installed is a third-party wrapper, not the official one you referenced in the docs. That’s probably why your import is failing!
To use the official connector correctly:
- Clone the BitMEX api-connectors repository:
git clone https://github.com/BitMEX/api-connectors.git - Navigate to the
official-ws/pythonfolder inside the cloned repo - Either:
- Add this folder to your PyCharm project and mark it as a Sources Root (right-click the folder > Mark Directory as > Sources Root), or
- Install it locally to your Python environment with:
pip install -e .
- Now your
from bitmex_websocket import BitMEXWebsocketimport should work, since this matches the official module structure.
One of these steps should resolve your ModuleNotFoundError. Let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者Ben Wallace

