You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac M1环境下Python 3.8.9脚本无法导入模块的求助

Python模块导入失败问题排查求助

导入任何Python模块都会报错,示例代码及错误如下:

from flask import Flask

报错信息:

ModuleNotFoundError: No module named 'flask'

将flask替换为psycopg2等其他模块,同样无法成功导入。怀疑问题与shell配置有关,但不确定具体原因及解决方法。

环境配置信息

  • Apple M1 Max
  • macOS: 12.5 (21G72) - Monterey
  • Terminal: iTerm2 - Build 3.4.16
  • 使用OhMyZsh
  • Python: 3.8.9
  • pip3: 22.1.2

已安装模块列表

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

补充信息

  1. where python执行结果:
    python: aliased to /usr/bin/python3
    
  2. where 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
    
  3. where pip执行结果:
    /Library/Frameworks/Python.framework/Versions/3.10/bin/pip
    /Library/Frameworks/Python.framework/Versions/3.10/bin/pip
    
  4. where 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
    

EDIT2补充

执行python3进入交互环境,版本为3.10.5,sys.path输出:

➜  ~ 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 show 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:

EDIT3补充shell配置文件信息

~目录下的相关文件:

1  staff    150 Jun 29 18:57 .bash_profile
1  staff    131 May 30 16:47 .bashrc
22  staff    704 May 30 13:32 .oh-my-zsh
1  staff    292 Jun 29 18:32 .zprofile
1  staff    126 Jun 17 10:19 .zprofile.pysave
1  staff  18097 Jul 28 09:53 .zsh_history
7  staff    224 Jul 13 13:38 .zsh_sessions
1  staff   4163 Jul 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"

.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"

恳请提供解决思路或方案。

内容的提问来源于stack exchange,提问作者Martin Carre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 06:24:22