python_pip_virtualenv - zollak/pentest-notes GitHub Wiki

Install Pip on MacOS

Install pip on MacOS, using easy_install command and upgrade pip to the latest version:

$ sudo easy_install pip
$ sudo pip install --upgrade pip

Python 2.7 details (used by MacOS):

$ python -V
Python 2.7.16

$ which python
/usr/bin/python

$ pip -V
pip 20.1 from /Library/Python/2.7/site-packages/pip (python 2.7)

$ which pip
/usr/local/bin/pip

$ python
Python 2.7.16 (default, Jan 27 2020, 04:46:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/Library/Python/2.7/site-packages/pycrypto-2.6.1-py2.7-macosx-10.11-intel.egg', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Users/username/Library/Python/2.7/lib/python/site-packages', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/opt/gtk/lib/python2.7/site-packages', '/opt/gtk/lib/python2.7/site-packages/gtk-2.0']

easy_install removes symlink for the existing pip3, however the output mentioned that it has been also installed.

Installing (or reinstall) python3 from the official site

Apple’s Mac OS comes with python 2.7 installed by default. Perhaps you may want to use python 3.x.x on your machine and also use pip for package management with python 3.x.x.

  1. Follow this link and download the latest python3 OS X package
  2. Run the package and follow the steps to install python3 on your computer.
  3. Once the installation is done, on have python3 with pip.
$ python3 -V
Python 3.8.2

$ which python3
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3

$ pip3 -V
pip 20.0.2 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)

$ which pip3
/Library/Frameworks/Python.framework/Versions/3.8/bin/pip3

$ python3
Python 3.8.2 (v3.8.2:7b3ab5921f, Feb 24 2020, 17:52:18)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload', '/Users/username/Library/Python/3.8/lib/python/site-packages', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages']

The application located in /Applications/Python\ 3.8/ folder.

Creating virtual environment on MacOS

virtualenv is a tool to create isolated Python environments. virtualenv creates a folder, which contains all the necessary executables to use the packages that a Python project would need.

You can install virtualenv with pip.

$ pip install virtualenv

WARNING: The script virtualenv is installed in '/Users/username/Library/Python/2.7/bin' which is not on PATH. Consider adding this directory to PATH.

You may check what is in your PATH:

$ echo $PATH | tr ':' '\n'

Add virtualenv dir into PATH:

$ export PATH="/Users/username/Library/Python/2.7/bin:$PATH"

Apply it for the current Terminal session:

$ source ~/.bash_profile
  1. Create virtualenv for Python 2.7:
$ virtualenv -p python2.7 .env

Activate your new virtual environment;

$ source .env/bin/activate

(.env) $ python -V
Python 2.7.16

$ pip -V
pip 20.0.2 from /path/where/you/are/.env/lib/python2.7/site-packages/pip (python 2.7)

(.env) MacBook-Pro:contagiodump zollak$ python
Python 2.7.16 (default, Jan 27 2020, 04:46:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/path/where/you/are/.env/lib/python2.7/site-packages']

Deactivate:

(.env) $ deactivate
  1. Create virtualenv for Python 3.x:
$ virtualenv -p python .env-3.8

Activate your new virtual environment;

$ source .env-3.8/bin/activate

(.env-3.8) $ python -V
Python 3.8.2

(.env-3.8) $ which python
/path/where/you/are/.env-3.8/bin/python

(.env-3.8) $ pip -V
pip 20.0.2 from /path/where/you/are/.env-3.8/lib/python3.8/site-packages/pip (python 3.8)

(.env-3.8) $ which pip
/path/where/you/are/.env-3.8/bin/pip

$ python
Python 3.8.2 (v3.8.2:7b3ab5921f, Feb 24 2020, 17:52:18)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload', '/path/where/you/are/.env-3.8/lib/python3.8/site-packages']

Deactivate:

(.env-3.8) $ deactivate
⚠️ **GitHub.com Fallback** ⚠️