繁体   English   中英

Pip3 - Python 3.8.9 - mac M1 - 我的脚本没有导入正常工作

[英]Pip3 - Python 3.8.9 - mac M1 - No import is working properly from my scripts

所以我正在努力解决什么将是,我敢肯定,一个简单的修复:无论我在我的 Python 脚本中导入什么,我总是会收到错误消息

from flask import Flask;
ModuleNotFoundError: No module named 'flask'

在这里用任何其他模块替换“flask”(尝试“psycopg2”),没有任何内容被导入。 我相信它与我的 shell 不知何故有关,但我不确定问题出在哪里,尤其是如何解决它。

以下是有关我的配置的更多信息:

  • 苹果 M1 Max
  • macOS :12.5 (21G72) - 蒙特雷
  • 终端:iTerm2 - Build 3.4.16
  • 使用OhMyZsh
  • Pyhton :V Python 3.8.9
  • pip3 :V pip 22.1.2

Pip 列表:

Package          Version
---------------- -------
click            8.1.3
Flask            2.1.3
Flask-SQLAlchemy 2.5.1
itsdangerous     2.1.2
Jinja2           3.1.2
MarkupSafe       2.1.1
pip              22.1.2
psycopg2         2.9.3
psycopg2-binary  2.9.3
setuptools       58.1.0
SQLAlchemy       1.4.39
Werkzeug         2.2.0

有任何想法吗?

编辑:

  • 其中 python:python:别名为 /usr/bin/python3

  • 其中python3:

    /Library/Frameworks/Python.framework/Versions/3.10/bin/python3 /opt/homebrew/bin/python3 /usr/local/bin/python3 /usr/bin/python3 /Library/Frameworks/Python.framework/Versions/3.10 /bin/python3 /opt/homebrew/bin/python3

  • 其中 pip

    /Library/Frameworks/Python.framework/Versions/3.10/bin/pip /Library/Frameworks/Python.framework/Versions/3.10/bin/pip

  • 在哪里 pip3

    /Library/Frameworks/Python.framework/Versions/3.10/bin/pip3 /opt/homebrew/bin/pip3 /usr/local/bin/pip3 /usr/bin/pip3 /Library/Frameworks/Python.framework/Versions/3.10 /bin/pip3 /opt/homebrew/bin/pip3

编辑2:

系统代码:

➜  ~ python3
Python 3.10.5 (v3.10.5:f377153967, Jun  6 2022, 12:36:10) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; print(sys.path)
['', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python310.zip', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages']

pip 显示 psycopg2:

➜  ~ pip show psycopg2
Name: psycopg2
Version: 2.9.3
Summary: psycopg2 - Python-PostgreSQL Database Adapter
Home-page: https://psycopg.org/
Author: Federico Di Gregorio
Author-email: fog@initd.org
License: LGPL with exceptions
Location: /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages
Requires:
Required-by:

编辑 3:配置文件和 shell 相关文件。

~ 文件夹:1 名员工 150 6 月 29 日 18:57.bash_profile 1 名员工 131 5 月 30 日 16:47.bashrc 22 名员工 704 5 月 30 日 13:32.oh-my-zsh 1 名员工 292 6 月 29 日 18:32.zprofile 1 名员工 126 6 月 17 日 10:19.zprofile.pysave 1 名员工 18097 7 月 28 日 09:53.zsh_history 7 名员工 224 7 月 13 日 13:38.zsh_sessions 1 名员工 4163 7 月 21 日 11:39.zshrc

猫 bash_profile:

source ~/.bashrc
export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
export PYTHONPATH="$PATH:/usr/local/bin/python"

cat.zprofile

export PATH="$PATH:/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
eval "$(/opt/homebrew/bin/brew shellenv)"

# Setting PATH for Python 3.10
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.10/bin:${PATH}"
export PATH

猫.zshrc

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="robbyrussell"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled  # disable automatic updates
# zstyle ':omz:update' mode auto      # update automatically without asking
# zstyle ':omz:update' mode reminder  # just remind me to update when it's time

# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)

source $ZSH/oh-my-zsh.sh
A
# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
alias python=/usr/bin/python3
export PATH="/opt/homebrew/opt/libpq/bin:$PATH"
export PATH="/opt/homebrew/opt/openssl@3/bin:$PATH"
export LIBRARY_PATH=$LIBRARY_PATH:/opt/homebrew/opt/openssl@3/lib/
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
export CPPFLAGS="-I/opt/homebrew/opt/libpq/include"

总结:使用

python -m pip install <packages>

保证python可以找到安装的包。


这是在同一系统上安装多个 Python 时经常出现的问题。 例如,可能有一个默认(系统)Python,以及用户自己安装的一个,因为该版本更新(或者他们需要特定版本;例如,Guido 禁止,Python 2,因为他们必须运行旧的 Python 脚本或使用旧的 Python 包)。

结果,有时在一些别名的帮助下,您最终可能会python指的是系统 Python 的情况,如下所示: /usr/bin/python 而例如python3可能指的是用户安装的 Python,如此处/opt/homebrew/bin/python3 这仍然很好,因为这些是不同的命令。

Enter pip , the Python package manager: it may be installed for only one of the multiple Python versions, or it may be installed for all of them, but each time with a different name. 因此, pip可能会为/usr/bin/python安装软件包,或者它可能会为/opt/homebrew/bin/python3安装软件包。 根据设置(以及sudo的使用,但更多内容见下文),这些包可能最终位于不同的位置。 And if the two Python versions are different enough, the installed packages will not be compatible, and will also be installed in different subdirectories of the package installation directory (on Linux, it would go in $HOME/.local/lib/pythonx.y , xy表示不同的版本。在 macOS 上,在$HOME/Library的某个地方。请注意,这适用于用户,即本地安装,没有sudo )。

To complicate things, the python 2-to-3 transition resulted in systems naming executables specifically python3 or pip3 , while python would still be Python 2. Now that most Python 2 (system) scripts have been ported to Python 3, that habit has worn关闭,Python 2 被抛弃了, python现在指的是 Python 3。但是您可能仍然被夹在中间,并且一些名称仍然存在。 令人困惑,(Python 3 应该被命名为完全不同的名称,例如monty 。这样可以避免很多混淆。)

所以,如果你幸运的话,一切正常。 如果你不走运,Pip 安装为错误的 Python 安装,这是一个漫长的搜索 Python 正确使用(或如何重新安装包)

可执行文件python确实可以选择将 package 作为脚本运行(简单地说): python -m <package>会这样做。 这将保证包/脚本使用python可执行文件。 Pip 是可以做到这一点的软件包之一(事实上, pippip3脚本是围绕该功能的简短包装)。

So using python -m pip install <package> guarantees that a package installed by pip, will be found by python as well. 不要搞乱不兼容pythonpip可执行文件。


我不建议在单用户系统上使用sudo 它可能会弄乱系统,因为很多人不知道他们系统的细节。 sudo最好只由系统管理员用于为系统上的其他用户安装软件。

Pip呢? Pip 有一个选项--user ,它会将软件包安装到 Python 将自动在用户的主目录中找到但与用户相关的目录中。 事实上,最新版本的 Pip 如果发现无法在正常(系统)位置安装软件包,则会自动使用该目录。 output 中会有一个注释,然后 pip 使用--user选项隐式安装。 这在 90%(或更多)的情况下是好的,因为这很可能是使用 Pip 的人的意图,而没有太多人知道--user


对于(正确安装和激活的)虚拟环境,这个问题不存在。 pythonpip在该虚拟环境中是独一无二的。 这包括一个 Conda 环境。

而且,实际上,Python 的内置虚拟环境创建是可运行模块的另一个示例:

python -m venv <name>

这也保证了您的虚拟环境使用(副本)该特定python可执行文件。 如果您想要一个具有另一个 Python 版本的虚拟环境,请使用例如

python3.8 -m venv <name>

(不要忘记之后激活相关的 v-env。),当未激活时,您将使用默认的python ,但激活时, python3.8 轻松切换

This doesn't work for all python executables: Python 2 doesn't have the venv package, and python executables on some systems are very barebones, and no venv package will exist. 通常,可以通过安装相关的 package 来修改(如果您使用的是系统 Python): sudo apt install python-venv或类似的东西(*)。 (另一种情况是sudo是否应该在单个用户系统上实际使用是值得商榷的,但更难避免。)


请注意,Python 版本始终可以通过使用添加到python可执行文件的 major.minor 版本来区分。 因此,如果做得好,一个系统可以让python2.7, python3.6, python3.7, python3.8, python3.9 and python3.10都相互独立地工作。

我实际上使用它偶尔使用pyenv切换和测试版本。 如果您不习惯管理 Python 版本,请注意不要打开另一罐蠕虫。

(*) 在我的选择中,有些系统将 pip 和 venv 与默认的 Python package 分开,这是很遗憾的,并且/或者默认不安装这些。 默认情况下安装这些需要很少的额外磁盘空间,并提供了极大的便利。 如果你想要一个准系统,它应该是简单地卸载系统包,如 python-pip 和 python-venv,而不是相反。


警告:虽然这在技术上适用于任何地方,但像 Visual Studio Code 这样的 IDE 可能仍然会引起新人的问题,因为它们都有一个终端,人们可能会使用另一个终端,然后是配置/设置本身,您可以在其中指定 Python解释器(有时还有 Pip 可执行文件)。

上述解决方案本身应该可以在命令行上正常工作。 Within an IDE, I can't guarantee this, but do check your settings for both Python and Pip, and beware of virtual environments set up (automatically) by your IDE, which may make things even worse, since the v-env will need在安装软件包之前激活(然后在运行 Python 时在下一个 session 中再次激活)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM