通过 pip 安装库的问题

发布于 2025-01-12 20:48:13 字数 4758 浏览 4 评论 0原文

我有一台新的 M1 Pro Mac,我正在尝试设置我的开发环境。我的 Mac 仍然默认为 python2,所以我必须在终端中将 python 3 引用为 $ python3

我知道 Python 带有一个预安装的模块来为您安装 pip,所以我运行了以下命令。

python3 -m ensurepip --upgrade
Defaulting to user installation because normal site-packages is not writeable
Looking in links: /var/folders/c8/cfg93ngn5lv63xqptw_mg8bm0000gn/T/tmp8m1wvvzr
Requirement already up-to-date: setuptools in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages (49.2.1)
Requirement already up-to-date: pip in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages (20.2.3)
danielschnoll@Daniels-MBP ~ % python3 pip
/Library/Developer/CommandLineTools/usr/bin/python3: can't open file 'pip': [Errno 2] No such file or directory'
danielschnoll@Daniels-MBP ~ % pip
zsh: command not found: pip
danielschnoll@Daniels-MBP ~ % pip3

Usage.....

然后我确保 pip 已升级,

danielschnoll@Daniels-MBP ~ % python3 -m pip install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
  Downloading pip-22.0.4-py3-none-any.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 3.0 MB/s 
Installing collected packages: pip
  WARNING: The scripts pip, pip3 and pip3.8 are installed in '/Users/danielschnoll/Library/Python/3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  NOTE: The current PATH contains path(s) starting with `~`, which may not be expanded by all applications.
Successfully installed pip-22.0.4

如果我执行 $ pip 它需要 $ pip3,pip 仍然无法工作。没什么大不了的。但现在我想安装 Flask,这样我就可以进行一些 API 开发。

danielschnoll@Daniels-MBP ~ % pip3 install flask
Defaulting to user installation because normal site-packages is not writeable
Collecting flask
  Downloading Flask-2.0.3-py3-none-any.whl (95 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 95.6/95.6 KB 1.9 MB/s eta 0:00:00
Collecting Jinja2>=3.0
  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.6/133.6 KB 2.9 MB/s eta 0:00:00
Collecting click>=7.1.2
  Downloading click-8.0.4-py3-none-any.whl (97 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.5/97.5 KB 3.6 MB/s eta 0:00:00
Collecting Werkzeug>=2.0
  Downloading Werkzeug-2.0.3-py3-none-any.whl (289 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 289.2/289.2 KB 6.2 MB/s eta 0:00:00
Collecting itsdangerous>=2.0
  Downloading itsdangerous-2.1.0-py3-none-any.whl (15 kB)
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.0-cp38-cp38-macosx_10_9_universal2.whl (17 kB)
Installing collected packages: Werkzeug, MarkupSafe, itsdangerous, click, Jinja2, flask
  WARNING: The script flask is installed in '/Users/danielschnoll/Library/Python/3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  NOTE: The current PATH contains path(s) starting with `~`, which may not be expanded by all applications.
Successfully installed Jinja2-3.0.3 MarkupSafe-2.1.0 Werkzeug-2.0.3 click-8.0.4 flask-2.0.3 itsdangerous-2.1.0

很好,Flask 已安装,如果我执行 $ pip3 list 就可以看到它,

danielschnoll@Daniels-MBP ~ % pip3 list
Package      Version
------------ -------
autopep8     1.6.0
click        8.0.4
Flask        2.0.3
itsdangerous 2.1.0
Jinja2       3.0.3
MarkupSafe   2.1.0
pip          22.0.4
pycodestyle  2.8.0
setuptools   49.2.1
six          1.15.0
toml         0.10.2
Werkzeug     2.0.3
wheel        0.33.1

但在我的 python 代码中,我无法导入 Flask。我的 pylinter 抛出错误,提示找不到 Flask。果然,如果我启动终端并打开Python3解释器,就会发生这种情况

danielschnoll@Daniels-MBP ~ % python3
Python 3.8.9 (default, Oct 26 2021, 07:25:53) 
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from Flask import flask
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'Flask'
>>> 

回顾我的安装历程,我发现了以下内容

WARNING: The script flask is installed in '/Users/danielschnoll/Library/Python/3.8/bin' which is not on PATH.

嗯,确实如此。我手动将其添加到我的 .bash_profile

export PATH="$PATH:/Library/Frameworks/Python.framework/Versions/3.8/bin"

与我的 .bashrc 相同

export PATH="$PATH:/Library/Frameworks/Python.framework/Versions/3.8/bin"
export PATH=$PATH:/usr/share/dotnet/dotnet

这是怎么回事?有人可以帮忙吗?谢谢

I have a new M1 Pro Mac and I'm trying to set up my dev environment. My Mac defaults to python2 still, so I have to reference python 3 as $ python3 in the terminal

I know Python comes with a preinstalled module to install pip for you, so I ran the following.

python3 -m ensurepip --upgrade
Defaulting to user installation because normal site-packages is not writeable
Looking in links: /var/folders/c8/cfg93ngn5lv63xqptw_mg8bm0000gn/T/tmp8m1wvvzr
Requirement already up-to-date: setuptools in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages (49.2.1)
Requirement already up-to-date: pip in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages (20.2.3)
danielschnoll@Daniels-MBP ~ % python3 pip
/Library/Developer/CommandLineTools/usr/bin/python3: can't open file 'pip': [Errno 2] No such file or directory'
danielschnoll@Daniels-MBP ~ % pip
zsh: command not found: pip
danielschnoll@Daniels-MBP ~ % pip3

Usage.....

I then made sure pip was upgraded

danielschnoll@Daniels-MBP ~ % python3 -m pip install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
  Downloading pip-22.0.4-py3-none-any.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 3.0 MB/s 
Installing collected packages: pip
  WARNING: The scripts pip, pip3 and pip3.8 are installed in '/Users/danielschnoll/Library/Python/3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  NOTE: The current PATH contains path(s) starting with `~`, which may not be expanded by all applications.
Successfully installed pip-22.0.4

pip still doesn't work if I do $ pip it needs $ pip3. Not a big deal. But now I want to install Flask so I can do some API development.

danielschnoll@Daniels-MBP ~ % pip3 install flask
Defaulting to user installation because normal site-packages is not writeable
Collecting flask
  Downloading Flask-2.0.3-py3-none-any.whl (95 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 95.6/95.6 KB 1.9 MB/s eta 0:00:00
Collecting Jinja2>=3.0
  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.6/133.6 KB 2.9 MB/s eta 0:00:00
Collecting click>=7.1.2
  Downloading click-8.0.4-py3-none-any.whl (97 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.5/97.5 KB 3.6 MB/s eta 0:00:00
Collecting Werkzeug>=2.0
  Downloading Werkzeug-2.0.3-py3-none-any.whl (289 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 289.2/289.2 KB 6.2 MB/s eta 0:00:00
Collecting itsdangerous>=2.0
  Downloading itsdangerous-2.1.0-py3-none-any.whl (15 kB)
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.1.0-cp38-cp38-macosx_10_9_universal2.whl (17 kB)
Installing collected packages: Werkzeug, MarkupSafe, itsdangerous, click, Jinja2, flask
  WARNING: The script flask is installed in '/Users/danielschnoll/Library/Python/3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  NOTE: The current PATH contains path(s) starting with `~`, which may not be expanded by all applications.
Successfully installed Jinja2-3.0.3 MarkupSafe-2.1.0 Werkzeug-2.0.3 click-8.0.4 flask-2.0.3 itsdangerous-2.1.0

Good, Flask is installed, and I see it if I do $ pip3 list

danielschnoll@Daniels-MBP ~ % pip3 list
Package      Version
------------ -------
autopep8     1.6.0
click        8.0.4
Flask        2.0.3
itsdangerous 2.1.0
Jinja2       3.0.3
MarkupSafe   2.1.0
pip          22.0.4
pycodestyle  2.8.0
setuptools   49.2.1
six          1.15.0
toml         0.10.2
Werkzeug     2.0.3
wheel        0.33.1

But in my python code, I can't import Flask. My pylinter throws errors saying Flask isn't findable. Sure enough if I launch the terminal and open the Python3 interpreter, this happens

danielschnoll@Daniels-MBP ~ % python3
Python 3.8.9 (default, Oct 26 2021, 07:25:53) 
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from Flask import flask
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'Flask'
>>> 

Looking back at my installation journey, I found the following

WARNING: The script flask is installed in '/Users/danielschnoll/Library/Python/3.8/bin' which is not on PATH.

Well, it is. I added it manually to my .bash_profile

export PATH="$PATH:/Library/Frameworks/Python.framework/Versions/3.8/bin"

Same with my .bashrc

export PATH="$PATH:/Library/Frameworks/Python.framework/Versions/3.8/bin"
export PATH=$PATH:/usr/share/dotnet/dotnet

What is going on here?? Can someone please assist? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文