MacOS下使用pip install --user安装plotly模块报错,求分步解决
Hey there, let's work through your Plotly installation problem step by step, since you're dealing with macOS High Sierra 10.13.3 and have both Python 2.7.10 (your default) and Python 3.6 installed. We'll start with fixing the Plotly install, then touch on switching your default Python version as a bonus.
Old versions of pip can cause all sorts of installation errors, so let's make sure both your Python 2.7 and 3.6 pip tools are up to date:
- For Python 2.7 (your current default):
You'll need to enter your macOS user password when prompted—this is normal for admin-level updates.sudo pip install --upgrade pip - For Python 3.6:
sudo pip3 install --upgrade pip
Since your python command defaults to 2.7, the regular pip will install Plotly there. If you want it for Python 3.6, use pip3 instead:
- Install for Python 2.7:
pip install plotly - Install for Python 3.6:
pip3 install plotly
Troubleshooting Common Errors
If you run into permission issues (no write access to system directories), skip the sudo and use the --user flag instead:
# For Python 2.7 pip install plotly --user # For Python 3.6 pip3 install plotly --user
If you're hitting network timeouts or slow downloads, you can use a trusted PyPI mirror by appending -i [mirror URL] to the install command—just replace [mirror URL] with a reliable mirror address you prefer.
Let's confirm Plotly is installed correctly:
- For Python 2.7:
Open terminal, typepython, then run these lines:
If you see a version number (likeimport plotly print(plotly.__version__)5.15.0) without errors, you're good to go. - For Python 3.6:
Typepython3in terminal, then run the same code snippet above.
Since you mentioned wanting to fix this later, here's a safe way to make Python 3.6 your default when you type python:
- Open your shell profile file:
- If you use Bash (default on High Sierra):
open ~/.bash_profile - If you use Zsh (if you've switched):
open ~/.zshrc
- If you use Bash (default on High Sierra):
- Add this line at the bottom of the file (replace the path with the output of
which python3if yours is different):alias python="/usr/local/bin/python3" - Save the file and close it, then apply the change immediately with:
source ~/.bash_profile # Or source ~/.zshrc if you edited that file - Test it by running
python --version—it should now show Python 3.6.x.
内容的提问来源于stack exchange,提问作者Antoine

