简体   繁体   中英

How to correctly install python3 and virtualenv on MacOS Mojave?

I start to learn Django framework so I need to install latest python, pip, virtualenv and django packets on my mac. I try to do it with brew, but I got some strange behavior.

At first, python3 installed not in /usr/bin/ but in /Library/Frameworks/Python.framework directory:

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

It is strange for me, because every tutorial tells about /usr/bin/python37 and nothing about /Library/Frameworks/Python.framework Is this okay?

After that I made sudo pip3 install virtualenv and got this answer:

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.

Okay, I made uninstall and install with -H sudo flag:

Installing collected packages: virtualenv
Successfully installed virtualenv-16.4.3

But when I try to make a virtual environment, I got

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

Checking virtualenv location:

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

Why /Library/Frameworks/Python.framework/? And why it searches for virtualenv in /usr/local/bin/virtualenv? Coding on Macos is always so painful?

Instead of using brew you can simply use "venv".

To create a virtual environment you can run -->

python3 -m venv environment_name

Example: If you want to create an virtual environment for django with name django_env

python3 -m venv django_env

"-m" flag checks for sys.path and executes main module.

Activation of Virtual Environment :

source django_env/bin/activate

Deactivation :

deactivate

Python3 Virtualenv Setup

Requirements:

  • Python3
  • Pip3
$ brew install python3 #upgrade

Pip3 is installed with Python3

Installation

To install virtualenv via pip run:

$ pip3 install virtualenv

Usage

Creation of virtualenv:

$ virtualenv -p python3 <desired-path>

Activate the virtualenv:

$ source <desired-path>/bin/activate

Deactivate the virtualenv:

$ deactivate

You can see more about the Homebrew on the official page .

Just follow as below:

  1. $ pip install virtualenv Once installed, you can create a virtual environment with:

  2. $ virtualenv [directory] On MacOS, we activate our virtual environment with the source command. If you created your venv in the myvenv directory, the command would be

  3. $ source myvenv/bin/activate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM