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

时间:2019-04-02 19:28:22

标签: python python-3.x macos virtualenv homebrew

我开始学习Django框架,因此需要在Mac上安装最新的python,pip,virtualenv和django数据包。 我尝试用brew做,但是有一些奇怪的行为。

首先,python3不在/ usr / bin /中安装,而是在/Library/Frameworks/Python.framework目录中安装:

sudo add-apt-repository ppa:webupd8team/java
sudo apt update
sudo apt install oracle-java8-set-default

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

此后,我做了$ which python /usr/bin/python $ which python3 /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 并得到了以下答案:

sudo pip3 install virtualenv

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

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.

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

Installing collected packages: virtualenv
Successfully installed virtualenv-16.4.3

检查virtualenv位置:

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

为什么/Library/Frameworks/Python.framework/? 为什么在/ usr / local / bin / virtualenv中搜索virtualenv? Macos上的编码总是那么痛苦吗?

2 个答案:

答案 0 :(得分:1)

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

要创建虚拟环境,可以运行->

getCurrentSession()

示例:如果要为名称为django_env的django创建虚拟环境

python3 -m venv environment_name

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

激活虚拟环境:

python3 -m venv django_env

停用:

source django_env/bin/activate

答案 1 :(得分:0)

Python3 Virtualenv设置

要求:

  • Python3
  • Pip3
$ brew install python3 #upgrade

Pip3与Python3一起安装

安装

要通过pip运行virtualenv,请执行以下操作:

$ pip3 install virtualenv

用法

创建virtualenv:

$ virtualenv -p python3 <desired-path>

激活virtualenv:

$ source <desired-path>/bin/activate

停用virtualenv:

$ deactivate

您可以在official page上查看有关Homebrew的更多信息。