Best Python Setup For Mac

Posted By admin On 16.02.22

Mac OS X comes with Python 2.7 out of the box.

You do not need to install or configure anything else to use Python 2. Theseinstructions document the installation of Python 3.

The version of Python that ships with OS X is great for learning, but it’s notgood for development. The version shipped with OS X may be out of date from theofficial current Python release,which is considered the stable production version.

After trying multiple editors and IDE’s in my last 12 years coding with python, I found Vim to be my best buddy. It works everywhere! (not just mac) Trust me today you are coding on Mac and tomorrow you might have to code and run debugger on some. Nov 24, 2018  Python Setup and Usage. Your best way to get started with Python on Mac OS X is through the IDLE integrated development environment, see section The IDE and use the Help menu when the IDE is running. There are several options for building GUI applications on the Mac with Python.

Doing it Right¶

Let’s install a real version of Python.

Before installing Python, you’ll need to install GCC. GCC can be obtainedby downloading Xcode, the smallerCommand Line Tools (must have anApple account) or the even smaller OSX-GCC-Installerpackage.

Note

If you already have Xcode installed, do not install OSX-GCC-Installer.In combination, the software can cause issues that are difficult todiagnose.

Note

If you perform a fresh install of Xcode, you will also need to add thecommandline tools by running xcode-select--install on the terminal.

While OS X comes with a large number of Unix utilities, those familiar withLinux systems will notice one key component missing: a package manager.Homebrew fills this void.

To install Homebrew, open Terminal oryour favorite OS X terminal emulator and run

The script will explain what changes it will make and prompt you before theinstallation begins.Once you’ve installed Homebrew, insert the Homebrew directory at the topof your PATH environment variable. You can do this by adding the followingline at the bottom of your ~/.profile file

If you have OS X 10.12 (Sierra) or older use this line instead

Now, we can install Python 3:

This will take a minute or two.

Pip¶

Homebrew installs pip pointing to the Homebrew’d Python 3 for you.

Working with Python 3¶

At this point, you have the system Python 2.7 available, potentially theHomebrew version of Python 2 installed, and the Homebrewversion of Python 3 as well.

will launch the Homebrew-installed Python 3 interpreter.

will launch the Homebrew-installed Python 2 interpreter (if any).

will launch the Homebrew-installed Python 3 interpreter.

If the Homebrew version of Python 2 is installed then pip2 will point to Python 2.If the Homebrew version of Python 3 is installed then pip will point to Python 3.

The rest of the guide will assume that python references Python 3.

Pipenv & Virtual Environments¶

The next step is to install Pipenv, so you can install dependencies and manage virtual environments.

A Virtual Environment is a tool to keep the dependencies required by different projectsin separate places, by creating virtual Python environments for them. It solves the“Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keepsyour global site-packages directory clean and manageable.

For example, you can work on a project which requires Django 1.10 while alsomaintaining a project which requires Django 1.8.

So, onward! To the Pipenv & Virtual Environments docs!

This page is a remixed version of another guide,which is available under the same license.

Brief: This guide shows you how to set up Python environment on Linux and other Unix-like systems.

If you’ve ever tried to set up a Python development environment in Windows, you know how challenging it can be. Recently, Python released a new version of their installers that have made that process nearly painless, but that doesn’t mean you get the best development environment out of the box, so, in the spirit of a recent post on It’s Foss about setting up a C++ environment, here’s how to do the same for Python.

Great news, Python’s already there

As a *nix user (because this applies to OsX as well) you already have some version of Python installed on your system. In fact, it’s probably a large part of how your package installer works. The real issue is finding out which Python version you have installed by default, and which Python version you are planning on programming with. So open a terminal and check what you have:

python --version

will return either:

Python3.x.x

or

Python 2.x.x

depending on what you get back, I’d also suggest trying the other release as well, by appending that number to the python command. In my case, the default Python install is 2, so I type:

python3 --version

and get back the appropriate Python 3.x.x response.

This will be important聽because it will determine how we run our Python code from whatever interpreter we end up using. There’s a whole different article to be written about changing your default Python install, so I’m going to avoid that discussion here. Just remember which one your machine defaults to, and which one you are wanting to target.

If you are missing one or the other, or if you find you are running an older version, just install the newest:

sudo apt-get install python *or* python#

Environments matter

One of the great things about Python is that it is fantastically simple to get working; this simplicity is also one of the pitfalls. Setting up a proper environment for working is going to be important, and can be confusing at first, because you might think you are ready to write with it simply installed on your machine.

You have to remember that for any version of Python, you are going to need to deploy that same setup to your production environment. Any of the packages you get from the package index, for example, will need to be installed on your production machine as well. It’s a good idea to keep track of these in a text file that can be used by pip to install them later.

The first thing to do is set up a virtual environment.

Python 2

In Python 2 you’ll want to install virtualenv using pip:

pip install virtualenv

if you get an error here, saying that you need to install pip first, go ahead and do so. Pip’s the most reliable way to manage packages, and like the link above says, it’s the recommended way to do it also. (hint for OS X users that got here, try sudo easy_install pip, you might have to use the command as pip2 instead of pip, just check for the –version)

With virtualenv installed, you can just cd to your project directory, and then make a new environment:

Install python 3 on mac

virtualenv [name_of_your_project]

this makes a bin of python files inside the current directory called my_project. That’s it, jump down to “Using your virtual environment” to see what to do next.

Python 3

In Python 3, the virtual environment module may need to be installed.

sudo apt-get install python3-venv

Once you have it, just cd into your project directory and run this command:

python -m venv [name_of_your_project]

this makes a bin of python files inside the current directory called my_project.

Using your Python virtual environment

With your environment installed, the procedure is pretty much the same in both versions of Python. I’ve included the working directory in the following commands for clarity.

[email protected]/to/my_dir$ source my_project/bin/activate
(my_project) [email protected]/to/my_dir$

Basically, what this command is doing is using that local, clean install of Python in your virtual environment to run your commands. To test this, you could run your python interpreter from inside the environment and try to import a module (numpy for example) you know you have on your main install of python.

To get back out of the environment:

(my_project) [email protected]/to/my_dir$ deactivate

[email protected]/to/my_dir$

Whenever you are in your project as source, remember that you are going to be changing that source environment, but not your main environment, so anything you do to that Python is limited to that environment.

Making your Python environment worthwhile

While you are working, you’ll occasionally want to export a list of the environment packages, to be able to install the same environment packages on your production machine.

(my_project) [email protected]_dir$ pip freeze > requirements.txt

Doing this will create a text file inside your project directory which will act as a list of all the Python packages you have installed in that environment. This way, when you put your project on your production machine, you simply have to run:

pip install -r requirements.txt

Run Python programs in Linux

Now that we’ve got the development environment setup properly, we can test it by writing some simple python code. I use vim to write code, so that’s where you’ll see me start this next bit of Python3 code, then run it. Keep in mind that django isn’t installed on my main machine, just on the source.

Switch Python Versions Mac

import django
print('Got here')

So basically, you just need to use the command in below fashion to run a Python program in Linux:

python program-name.py

Sorry, I had to change environments for this last gif, but you get the picture. Note that I’m in (my_project) as source when I run this the first time, and then I get the failure when I’m out of (my_project) as source.

There are a bundle of IDEs out there, and most of them handle this kind of thing well, if you pay attention to what you are doing. Just remember that the install of python inside of your project is the one you want to use to run your code.

Big Caveat

Since I made the mistake, at a younger age of doing *nix stuff, I’m going to impart some wisdom here. Do not run any of the pip commands as sudo. You’ll mess up your main install of Python, and that will mess up your Linux package manager… and basically, it will ruin your day. I lost a whole Mint install to this once, so just remember not to sudo this stuff.

Best Python Setup For Mac

If you are interested,you sould also learn to use pip on Ubuntu.

Mac Upgrade Python 3