今日运维之-Mac笔记本python环境问题

1. 问题:MAC升级系统后git报错?

bash 复制代码
Error: Can't create update lock in /usr/local/var/homebrew/locks!
Fix permissions by running:
  sudo chown -R $(whoami) /usr/local/var/homebrew
Traceback (most recent call last):
	11: from /usr/local/Homebrew/Library/Homebrew/brew.rb:23:in `<main>'
	10: from /usr/local/Homebrew/Library/Homebrew/brew.rb:23:in `require_relative'
	 9: from /usr/local/Homebrew/Library/Homebrew/global.rb:29:in `<top (required)>'
	 8: from /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 7: from /usr/local/Homebrew/Library/Homebrew/vendor/portable-ruby/2.6.3/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	 6: from /usr/local/Homebrew/Library/Homebrew/os.rb:3:in `<top (required)>'
	 5: from /usr/local/Homebrew/Library/Homebrew/os.rb:21:in `<module:OS>'
	 4: from /usr/local/Homebrew/Library/Homebrew/os/mac.rb:58:in `prerelease?'
	 3: from /usr/local/Homebrew/Library/Homebrew/os/mac.rb:24:in `version'
	 2: from /usr/local/Homebrew/Library/Homebrew/os/mac.rb:24:in `new'
	 1: from /usr/local/Homebrew/Library/Homebrew/os/mac/version.rb:26:in `initialize'
/usr/local/Homebrew/Library/Homebrew/version.rb:368:in `initialize': Version value must be a string; got a NilClass () (TypeError)

解决办法:

以上错误因升级系统后开发工具需要更新,于是使用以下命令更新工具,需要大概15-20分钟不等,看网络环境

bash 复制代码
xcode-select --install

2. 问题:多个python版本如何配置?

安装python新的包:MAC中安装过多个版本的python,3.6,3.7,3.8,希望用较新的的版本

解决办法:

将对应/usr/local/bin/目录下的python3,pip3文件,替换成python3.8对应的快捷方式即可。

输入命令检查默认python3是3.8了,成功后,看到python3和pip3版本都是3.8

pip3 --version

3. 问题:pip3 intall报错pg_config executable not found.如何解决?

pip3 install -r /xxx/.../requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple

遇到报错

bash 复制代码
 ERROR: Command errored out with exit status 1:
     command: /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/wq/kx6ydl213rl_fw7bv3m5sbnm0000gs/T/pip-install-vjae8obl/psycopg2/setup.py'"'"'; __file__='"'"'/private/var/folders/wq/kx6ydl213rl_fw7bv3m5sbnm0000gs/T/pip-install-vjae8obl/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base pip-egg-info
         cwd: /private/var/folders/wq/kx6ydl213rl_fw7bv3m5sbnm0000gs/T/pip-install-vjae8obl/psycopg2/
    Complete output (23 lines):
    running egg_info
    creating pip-egg-info/psycopg2.egg-info
    writing pip-egg-info/psycopg2.egg-info/PKG-INFO
    writing dependency_links to pip-egg-info/psycopg2.egg-info/dependency_links.txt
    writing top-level names to pip-egg-info/psycopg2.egg-info/top_level.txt
    writing manifest file 'pip-egg-info/psycopg2.egg-info/SOURCES.txt'
    
    Error: pg_config executable not found.
    
    pg_config is required to build psycopg2 from source.  Please add the directory
    containing pg_config to the $PATH or specify the full executable path with the
    option:
    
        python setup.py build_ext --pg-config /path/to/pg_config build ...
    
    or with the pg_config option in 'setup.cfg'.
    
    If you prefer to avoid building psycopg2 from source, please install the PyPI
    'psycopg2-binary' package instead.
    
    For further information please check the 'doc/src/install.rst' file (also at
    <https://www.psycopg.org/docs/install.html>).
    
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

这个报错,缺少必要的系统依赖

psycopg2 是一个 PostgreSQL 数据库的适配器,它在编译安装时需要一些系统级的开发库。

解决办法

  • macOS:可以使用 Homebrew 来安装所需的依赖。打开终端并执行以下命令:

    brew install postgresql

4. 问题:brew安装库没权限如何解决?

bash 复制代码
Error: Can't create update lock in /usr/local/var/homebrew/locks!
Fix permissions by running:

解决办法:

1. 修复权限

根据错误提示,通过运行特定命令来修复 /usr/local/var/homebrew/locks 目录的权限。以下是具体步骤:

  • 打开终端 :在 macOS 中,通过 "聚焦搜索"(按下 Command + Space 组合键),输入 "终端" 并打开它。

  • 执行权限修复命令 :在终端中输入以下命令并回车,该命令会将 /usr/local/var/homebrew/locks 目录的所有者设置为当前用户:

    sudo chown -R $(whoami) /usr/local/var/homebrew/locks

执行该命令后,系统会提示你输入管理员密码。输入密码时,终端不会显示任何字符,输入完成后按下回车键即可。

2. 检查 Homebrew 目录整体权限

有时,除了 locks 目录,Homebrew 的其他目录也可能存在权限问题。你可以通过以下命令修复 Homebrew 相关目录的权限:

复制代码
sudo chown -R $(whoami) /usr/local/Homebrew
sudo chown -R $(whoami) /usr/local/Caskroom
sudo chown -R $(whoami) /usr/local/Cellar
sudo chown -R $(whoami) /usr/local/Frameworks
sudo chown -R $(whoami) /usr/local/lib
sudo chown -R $(whoami) /usr/local/opt
sudo chown -R $(whoami) /usr/local/sbin
sudo chown -R $(whoami) /usr/local/share
sudo chown -R $(whoami) /usr/local/etc

如果有些目录不存在应该也可以暂时不必管。后面需要再操作一次命令。

3. 重新执行 Homebrew 操作

完成权限修复后,你可以再次尝试执行之前失败的 Homebrew 操作,例如更新 Homebrew:

复制代码
brew update

通过以上步骤,你应该能够解决 Homebrew 权限问题,使 Homebrew 操作正常进行。如果问题仍然存在,可能需要进一步检查系统的安全设置或文件系统是否存在异常。

5. 问题:brew update很慢怎么切换镜像?

解决办法:

bash 复制代码
sudo chown -R $(whoami) $(brew --prefix)/*
bash 复制代码
cd "$(brew --repo)"

git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"

git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
bash 复制代码
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile

source ~/.bash_profile

brew update

再次安装brew install postgresql,即可成功

安装完成后再次运行pip3 install命令:

pip3 install -r /xx/x.../requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple又又报错了

6. 问题:报错pyproject.toml无法安装相关python库

bash 复制代码
ERROR: Exception:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 188, in main
    status = self.run(options, args)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_internal/commands/install.py", line 345, in run
    resolver.resolve(requirement_set)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_internal/legacy_resolve.py", line 196, in resolve
    self._resolve_one(requirement_set, req)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_internal/legacy_resolve.py", line 359, in _resolve_one
    abstract_dist = self._get_abstract_dist_for(req_to_install)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_internal/legacy_resolve.py", line 305, in _get_abstract_dist_for
    abstract_dist = self.preparer.prepare_linked_requirement(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_internal/operations/prepare.py", line 214, in prepare_linked_requirement
    abstract_dist.prepare_distribution_metadata(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_internal/distributions/source.py", line 30, in prepare_distribution_metadata
    self.req.load_pyproject_toml()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_internal/req/req_install.py", line 501, in load_pyproject_toml
    pyproject_toml_data = load_pyproject_toml(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_internal/pyproject.py", line 66, in load_pyproject_toml
    pp_toml = pytoml.load(f)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_vendor/pytoml/parser.py", line 11, in load
    return loads(fin.read(), translate=translate, object_pairs_hook=object_pairs_hook, filename=getattr(fin, 'name', repr(fin)))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_vendor/pytoml/parser.py", line 24, in loads
    ast = _p_toml(src, object_pairs_hook=object_pairs_hook)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_vendor/pytoml/parser.py", line 340, in _p_toml
    s.expect_eof()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_vendor/pytoml/parser.py", line 125, in expect_eof
    return self._expect(self.consume_eof())
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip/_vendor/pytoml/parser.py", line 165, in _expect
    raise TomlError('msg', self._pos[0], self._pos[1], self._filename)
pip._vendor.pytoml.core.TomlError: /private/var/folders/wq/kx6ydl213rl_fw7bv3m5sbnm0000gs/T/pip-install-rwhgygf_/bcrypt/pyproject.toml(62, 1): msg

解决办法:

升级pip版本:

pip3 install --upgrade pip -i https://pypi.mirrors.ustc.edu.cn/simple

升级后可以继续安装了,终于安装好了python的库。

pip3 install -r /xx/x.../requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple

相关推荐
用户27784491049933 小时前
借助DeepSeek智能生成测试用例:从提示词到Excel表格的全流程实践
人工智能·python
咖啡教室3 小时前
日常开发中常用的git操作命令和使用技巧
git
JavaEdge在掘金5 小时前
ssl.SSLCertVerificationError报错解决方案
python
我不会编程5556 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
宁zz6 小时前
乌班图安装jenkins
运维·jenkins
老歌老听老掉牙6 小时前
平面旋转与交线投影夹角计算
python·线性代数·平面·sympy
满怀10156 小时前
Python入门(7):模块
python
无名之逆6 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
大丈夫立于天地间6 小时前
ISIS协议中的数据库同步
运维·网络·信息与通信
你觉得2056 小时前
哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义下载方法
大数据·人工智能·python·gpt·学习·机器学习·aigc