如何在MacOS上正确安装和运行virtualenv

问题描述 投票:0回答:2

嗨,我是 python 的初学者,我不记得何时以及如何在 Macbook Air 上安装 python3.8,只知道安装路径:

% which python
/usr/bin/python
% which python3
/usr/local/bin/python3

找不到pip命令,但pip3可以。今天我想安装 virtaulenv:

% sudo -H pip3 install virtualenv
WARNING: Ignoring invalid distribution - (/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages)
Successfully installed virtualenv-20.6.0

我可以使用“pip3 show virtualenv”来了解信息:

% pip3 show virtualenv
Name: virtualenv
Version: 20.6.0
Summary: Virtual Python Environment builder
Home-page: https://virtualenv.pypa.io/
Author: Bernat Gabor
Author-email: [email protected]
License: MIT
Location: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages
Requires: six, platformdirs, backports.entry-points-selectable, distlib, filelock
Required-by:

但是当我使用“virtualenv”时,我收到命令未找到消息,然后我“pip3卸载”它。我搜索了这个并得到了使用“easy_install”安装 virtualenv 的提示。安装后我可以执行命令,但收到一些错误消息:

% virtualenv                     
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 3241, in <module>
    @_call_aside
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 3225, in _call_aside
    f(*args, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 3254, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 583, in _build_master
    ws.require(__requires__)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 900, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 786, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'importlib-resources>=1.0' distribution was not found and is required by virtualenv

它似乎不起作用,我尝试“sudo pip3 uninstall virtualenv”成功卸载它,但它似乎并没有真正被删除:

% which virtualenv              
/usr/local/bin/virtualenv

我对此一无所知,你能帮我吗?我只想正常运行 virtualenv 并创建 Flask 项目...

PS。我可以“sudo pip3 installflask”和“pip3showflask”但仍然无法运行flask cammand(未找到命令),我该怎么办?

非常感谢!

python virtualenv
2个回答
18
投票

尝试明确您正在使用的 python 版本并使用

-m pip
进行安装

python3 -m pip install virtualenv
python3 -m virtualenv venv # create a new venv in ./venv
source ./venv/bin/activate # activate your new venv

很多时候,pip/pip3 并不指向您认为正在使用的同一个 python 版本...通过使用这种技术,您肯定会使用正确的 python 和 pip


0
投票

如果您这样做只是因为需要安装软件包,您可以随时使用自制程序:

brew install python3

brew install pipx

brew install your-pip-package

© www.soinside.com 2019 - 2024. All rights reserved.