Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 938
Trady Avatar asked Apr 02 '19 19:04

Trady


People also ask

Does Virtualenv work with Python 3?

Virtualenv is only installed on DreamHost servers for Python 2. If you're working with Python 3, you must install virtualenv using pip3. pip3 is not installed on the server by default. You must first install a custom version of Python 3.


1 Answers

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
like image 185
Chandra Sekhar Guptha Avatar answered Nov 14 '22 22:11

Chandra Sekhar Guptha