Skip to content

Ramblings and Thoughts

  • About

Ramblings and Thoughts

Networking, Computers, Life

Setting up Python the proper way on a new Mac

February 24, 2019 by chrisbog

A lot of people ask what the proper way of setting up the different versions of Python on a brand new Mac.   After discussing it with people, this pretty much sums it up.

http://python-guide-pt-br.readthedocs.io/en/latest/starting/install/osx/

Install HomeBrew

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

NOTE: When I tried this on my home MAC, I didn’t have to specify the path to ruby above.   I’m not sure really why that works or doesn’t work.

Install Python 2 (The current Mac OSX Python is Older)

$ brew install python

Install Python 3 (Not included on Mac OSX)

$ brew install python3

Now to access the Native Mac Version, you can type:

CBOGDON-M-D1FU:~ cbogdon$ python
Python 2.7.10 (default, Feb 6 2017, 23:53:20) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

To access the new HomeBrewed Python 2.7.x, do:

CBOGDON-M-D1FU:~ cbogdon$ python2
Python 2.7.13 (default, Apr 5 2017, 09:58:16) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

To access the Python 3.x do:

CBOGDON-M-D1FU:~ cbogdon$ python3
Python 3.6.1 (default, Apr 5 2017, 10:03:33) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Now we have a very segmented installation of Python with three versions: Native, New 2.x and New 3.x.

The key to keeping python clean is to install a virtual environment and do all the development in the virtual environment.   To do this, follow:

Now we can install virtualenv.

pip install virtualenv

We can also verify that the only package that is installed in the Mac is virtualenv

CBOGDON-M-D1FU:coding cbogdon$ pip freeze
virtualenv==15.1.0
CBOGDON-M-D1FU:coding cbogdon$

one style that seems to work is to have two different directory structures.   One for your code and then one for your virtual environments.   For example:

coding
virtualenv

Let’s say we have code stored in the coding directory called “merakiquery”.   To create the virtual environment, we can do:

cd coding/merakiquery
virtualenv ../../virtualenv/merakiquery
source ../../virtualenv/merakiquery/bin/activate
pip install -r requirements.txt

when completed, we can do:

deactivate

Any future time we want to run this, we can:

cd coding/merakiquery
source ../../virtualenv/merakiquery/bin/activate
pip install -r requirements.txt


Now that VirtualEnv is installed, we can also create the virtualenvironment through PyCharm.   We should just make sure we point to the right directory to create the virtualenv.

Post navigation

Previous Post:

Turning off referral of videos in Youtube

Next Post:

Changing Chrome to default to OSX’s System Print Dialog

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tags

Apache Carbon Copy Cloner Chrome Cisco Crashplan HTML iOS iClou Linux MySQL Nexus NXAPI Omnifocus OSX Outlook PyCharm Pytest Python Quicken Samsung TV Ubuntu VMWare Wordpress Youtube
© 2025 Ramblings and Thoughts | WordPress Theme by Superbthemes