Macos 如何将pythonpath更改为使用python 3.9和venv

Macos 如何将pythonpath更改为使用python 3.9和venv,macos,pythonpath,python-venv,Macos,Pythonpath,Python Venv,我正试图在运行OSX 10.15.7 Catalina的Mac上掌握Python 我的目标是能够使用venv在自己的虚拟环境中运行一个需要python 3.9和相关库的程序 我的探索让我走过了许多崎岖的道路和死巷,包括通过蟒蛇和酿酒 我正在运行zsh-特别是我的zsh-ys版本。如果这很重要的话。大多数在线论坛和教程都采用bash。我更愿意在zsh中完成所有工作,以避免同时编辑zsh和bash概要文件 当前外壳输出 $ python -V Python 2.7.16 $ which pytho

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

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

我的探索让我走过了许多崎岖的道路和死巷,包括通过蟒蛇和酿酒

我正在运行zsh-特别是我的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
$ 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
在那里暂停..

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
$ 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)
皮普呢?

$ 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
$ 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)
注意两点

$ 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
$ 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
1尽管出现了包装器警告,但PIP20.3.3是我升级的最新版本。下面几行可能是警告的原因,这表明正在从CL工具调用PIP3

2“正常站点包不可写”错误应该通过指定路径来解决。我已经尝试过指定正确的路径——见下文——但这并没有解决问题,这也是在本文末尾提出这个问题的另一个主要原因

$ 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
结束

最后谈谈Brew。许多教程建议使用Brew安装Python、pyenv和库等。我这样做并成功创建了一个测试环境,但未能使用它。然后,我尽可能多地删除,以恢复到“干净”状态来工作

当前brew状态为

$ 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
$ 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运行,而不是从任何其他版本或位置运行。我必须承认对那件事不太清楚

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

此时,关于如何创建虚拟环境的教程也让我感到困惑。根据我所读到的,我认为我应该使用venv而不是pyenv。不清楚的是我是否从根目录运行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

这就是我要做的:

# 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

$echo$PATH/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin如何更改$PATH中的顺序?您的$PATH看起来已经很好了,因为您有
/Library/Frameworks/Python.framework/Versions/3.9/bin
。当您键入
python-V
时,应该会得到类似于
python3.9.x
的内容,这就是我感到困惑的原因。如果你看到我的OP,你会注意到python-V返回python 2.7.16系统python,这是正常的,而python3-V返回python 3.8.2。@rico1更新了我的答案,更改为
/Library/Frameworks/python.framework/Versions/3.9/bin/python3.9
怪异。1$sudo[etc]命令似乎正在执行,但python3-V仍然返回python3.8.2。2定位python仍然返回/Library/Frameworks/python.framework/Versions/3.9/bin;但是3 Python.framwork在Finder中不可见(现在就忘了它以前是否可见)和$echo$PATH/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/sbin:/opt/X11/bin:/usr/local/git/bin如何更改$PATH中的顺序?您的$PATH似乎已经很好了,正如您所拥有的
/Library/Frameworks/Python.framework/Versions/3.9/bin
。当您键入
python-V
时,应该会得到类似于
python3.9.x
的内容,这就是我感到困惑的原因。如果你看到我的OP,你会注意到python-V返回python 2.7.16系统python,这是正常的,而python3-V返回python 3.8.2。@rico1更新了我的答案,更改为
/Library/Frameworks/python.framework/Versions/3.9/bin/python3.9
怪异。1$sudo[etc]命令似乎正在执行,但python3-V仍然返回python3.8.2。2定位python仍然返回/Library/Frameworks/python.framework/Versions/3.9/bin;但是3 Python.framwork在Finder中不可见(现在忘记它是否在之前),请编辑问题以删除大写。如果要突出显示某些内容,请使用粗体。请编辑问题以删除大写。如果要突出显示某些内容,请使用粗体。