如何在 MacOS Mojave 上正确安装 python3 和 virtualenv?

新手上路,请多包涵

我开始学习 Django 框架,所以我需要在我的 mac 上安装最新的 python、pip、virtualenv 和 django packets。我尝试用 brew 来做,但我有一些奇怪的行为。

起初,python3 没有安装在 /usr/bin/ 中,而是安装在 /Library/Frameworks/Python.framework 目录中:

 $ which python
/usr/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3

这对我来说很奇怪,因为每个教程都讲述了 /usr/bin/python37 而没有讲述 /Library/Frameworks/Python.framework 这样可以吗?

之后我做了 sudo pip3 install virtualenv 并得到了这个答案:

 The directory '/Users/user/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/user/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

好的,我使用 -H sudo 标志进行了卸载和安装:

 Installing collected packages: virtualenv
Successfully installed virtualenv-16.4.3

但是当我尝试创建一个虚拟环境时,我得到了

$ virtualenv venv
-bash: /usr/local/bin/virtualenv: No such file or directory

检查 virtualenv 位置:

 $ which virtualenv
/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenv

为什么是 /Library/Frameworks/Python.framework/?为什么它会在 /usr/local/bin/virtualenv 中搜索 virtualenv?在 Mac 上写代码总是那么痛苦?

原文由 Trady 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 502
1 个回答

除了使用 brew,您还可以简单地使用“venv”。

要创建一个虚拟环境,您可以运行 –>

 python3 -m venv environment_name

例子:如果你想为 django 创建一个名为 django_env 的虚拟环境

python3 -m venv django_env

“-m”标志检查 sys.path 并执行主模块。

激活虚拟环境:

 source django_env/bin/activate

停用:

 deactivate

原文由 Chandra Sekhar Guptha 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题