如何更改 pythonpath 以使用 python 3.9 和 venv

问题描述

我正在尝试在运行 OSX 10.15.7 Catalina 的 Mac 上掌握 Python。

我的目标是能够在自己的虚拟环境中使用 venv 运行需要 python 3.9 和相关库的程序。

我的探索使我走过了许多崎岖的小路和死胡同 - 包括通过 Anaconda 和 Brew。

我正在运行 zsh - 特别是 oh-my-zsh ys 版本。如果这很重要。大多数在线论坛和教程都假定使用 bash。我更愿意在 zsh 中完成所有操作,以避免同时编辑 zsh 和 bash 配置文件

当前壳输出

$ python -V
Python 2.7.16

$ which python
/usr/bin/python

$ python3 -V
Python 3.8.2

$ which python3.8
python3.8 not found

$ which python3.9
python3.9 not found

$ python3 -m -V
/Library/Developer/CommandLinetools/usr/bin/python3: No module named -V

停在那里..

Py 2.7 是 OSX 系统 python,我不想用它做任何事情。

奇怪的是 python3 -m 返回对 CLI 工具的引用。见下文。 py3.8 的唯一位置是在 CL 工具中。

我有哪些 python(下面的输出经过编辑以仅显示主目录和文件)?

$ locate python
/Applications/LibreOffice.app/Contents/Frameworks/LibreOfficePython.framework/Versions/3.7/bin/python3
/Library/Developer/CommandLinetools/Library/Frameworks/python3.framework/Versions/3.8/bin/python3.8
/Library/Developer/CommandLinetools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/Library/Developer/CommandLinetools/usr/bin/python3.8
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3-config
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9-config
/Library/Frameworks/Python.framework/Versions/3.9/share/man/man1/python3.1
/Library/Frameworks/Python.framework/Versions/3.9/share/man/man1/python3.9.1
/Library/Frameworks/SAVI.framework/Versions/A/Frameworks/Python.framework/Versions/3.6/lib/python3.6
/usr/bin/python
/usr/bin/python-config
/usr/bin/python2
/usr/bin/python2.7
/usr/bin/python2.7-config
/usr/bin/python3
/usr/bin/pythonw
/usr/bin/pythonw2.7
/usr/lib/libpython.dylib
/usr/lib/libpython2.7.dylib
/usr/lib/python2.7
/usr/local/bin/python3
/usr/local/bin/python3-config
/usr/local/bin/python3.9
/usr/local/bin/python3.9-config
/usr/share/file/magic/python
/usr/share/man/man1/python.1
/usr/share/man/man1/python2.7.1
/usr/share/man/man1/pythonw.1
/usr/share/man/man1/pythonw2.7.1
/usr/share/vim/vim81/autoload/python3complete.vim
/usr/share/vim/vim81/autoload/pythoncomplete.vim
/usr/share/vim/vim81/ftplugin/python.vim
/usr/share/vim/vim81/indent/python.vim
/usr/share/vim/vim81/Syntax/python.vim
/usr/share/zsh/5.7.1/functions/_bpython
/usr/share/zsh/5.7.1/functions/_python
/usr/share/zsh/5.7.1/functions/_python_modules

PIP 怎么样?

$ pip3
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.

$ pip3 install --upgrade pip
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pip in ./Library/Python/3.8/lib/python/site-packages (20.3.3)

注意两点

1 虽然出现包装警告,但 PIP 20.3.3 是我升级的最新版本。警告的原因可能在下几行,这表明正在从 CL 工具调用 PIP3。

2 'normal site packages not writable' 错误应该通过指定路径来解决。我已经尝试指定正确的路径 - 见下文 - 但这并没有解决问题,并且是在本文末尾提出问题的另一个主要原因。

$ python -m pip
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named pip

$ python3 -m pip
Usage:   
  /Library/Developer/CommandLinetools/usr/bin/python3 -m pip <command> [options]

$ which pip3
/usr/bin/pip3

$ locate pip3
/Applications/LibreOffice.app/Contents/Frameworks/LibreOfficePython.framework/Versions/3.7/bin/pip3
/Applications/LibreOffice.app/Contents/Frameworks/LibreOfficePython.framework/Versions/3.7/bin/pip3.7
/Library/Developer/CommandLinetools/usr/bin/pip3
/Library/Developer/CommandLinetools/usr/bin/pip3.8
/Library/Frameworks/Python.framework/Versions/3.9/bin/pip3
/Library/Frameworks/Python.framework/Versions/3.9/bin/pip3.9
/usr/bin/pip3
/usr/local/bin/pip3
/usr/local/bin/pip3.9

系统文件

~/.zprofile 内容 - 编辑了几次以删除对 virfualenv 和 penv 的引用,它们最初与 brew 一起安装,现在已删除。我已经运行 brew uninstall 和 brew remove 来尝试清理所有内容,但可能没有成功。

开始

# THIS FILE COMMENTED OUT RH 21DEC20
## Setting PATH for Python 3.9
## The original version is saved in .zprofile.pysave. **Note in this post only - cannot find this file**

PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
export PATH  **Note in this post only - this does not seem to have worked - see above python3 -V**

# export WORKON_HOME=~/.virtualenvs
# mkdir -p $WORKON_HOME
# . ~/.pyenv/versions/3.9.0/bin/virtualenvwrapper.sh
# eval "$(pyenv init -)"

# # AND THESE LInes ADDED FOLLOWING STACKOVERFLOW Q 33216679  **Note in this post only - commented out as shell threw errors .’cannot find virtualenvwrapper.sh**

# export WORKON_HOME=$HOME/.Envs
# # export VIRTUALENVWRAPPER_PYTHON=$/usr/bin/python3
# export VIRTUALENVWRAPPER_PYTHON=$/usr/local/bin/python3 
#source $HOME/.local/bin/virtualenvwrapper.sh

结束

最后说一下 Brew。 很多教程都建议使用 Brew 来安装 Python、pyenv 和库等。我这样做并成功创建了一个测试环境,但无法使用它。然后我尽可能多地移除,以便恢复到“干净”的状态。

当前的酿造状态是

$ brew doctor

Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they Could cause problems when
building Homebrew formulae,and may need to be deleted.

Unexpected dylibs:
  /usr/local/lib/libdvdcss.2.dylib
  /usr/local/lib/libfuse.0.dylib
  /usr/local/lib/libulockmgr.0.dylib

Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they Could cause problems when
building Homebrew formulae,and may need to be deleted.

Unexpected header files:
  /usr/local/include/ulockmgr.h

Warning: Unbrewed .la files were found in /usr/local/lib.
If you didn't put them there on purpose they Could cause problems when
building Homebrew formulae,and may need to be deleted.

Unexpected .la files:
  /usr/local/lib/libulockmgr.la

Warning: Unbrewed static libraries were found in /usr/local/lib.
If you didn't put them there on purpose they Could cause problems when
building Homebrew formulae,and may need to be deleted.

Unexpected static libraries:
  /usr/local/lib/libfuse.a
  /usr/local/lib/libulockmgr.a

总结

我的第一个目标是将 Py3.9.x 设置为新项目的认值。我认为这也需要/暗示 pip 和其他命令从 v3.9 运行,而不是任何其他版本或位置。必须承认对此很模糊。

然后我希望能够创建一个虚拟环境,我可以在其中放置我想要运行的程序及其相关的库和包。这样做意味着使用终端,但我需要知道我需要在哪个目录中运行哪些命令。

此时,我也对如何创建虚拟环境的教程感到困惑。从我读过的内容来看,我认为我应该在 pyenv 上使用 venv。不清楚的是我是从 root 运行 venv 命令,还是我必须创建并 cd 到不同的位置,如果是这样,该怎么办。

任何帮助

1 将认 python 更改为 3.9

2 创建 venv 以使用 v3.9。

3 在何处运行什么命令。

将不胜感激。请保持简单,不要假设任何先验知识!

祝大家圣诞节快乐。

解决方法

这就是我会做的:

# For 1 Changing the default python to 3.9
cd /usr/local/bin
sudo ln -fs /Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 python3
sudo ln -fs python3 python

# And make sure /usr/local/bin is before (/usr/bin and /Library/Developer/CommandLineTools/usr/bin) in your $PATH

# For 2 Creating a venv to use v3.9
/usr/local/bin/python3.9 -m venv /tmp/python3.9-venv

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...