Mac Python Environment - mikec964/chelmbigstock GitHub Wiki

I'm setting up to write games in Python using Pygame on my Mac and Raspberry Pi. Here's how I set it up, mostly on the Mac, but with some tips for Pi and Ubuntu.

Let's start with setting up our programming environment. The major features are:

And a good guide to Python: http://docs.python-guide.org/en/latest/

Sublime Text

On the Mac, I prefer Sublime Text 3. From this page, I like to add these to Sublime Text:

And add this to .bash_rc:

export GIT_EDITOR="subl --wait --new-window"
alias subl="'/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl'"

Gedit

On Raspberry Pi, I'm trying Gedit. Use sudo apt-get install gedit.

Brew

Homebrew is a package manager.

  • http://docs.python-guide.org/en/latest/starting/install/osx/
    • Install Command Line tools (search apple dev site for 'command line tools')
  • Make yourself an Admin for the Brew install command
    • If you get a certificate problem (on a recent Mac OS), it may be using the wrong curl. Type which curl and be sure it's /usr/bin/curl.
    • run brew update and brew doctor to start troubleshooting.
    • Brew docs
  • This installs pip for you
  • Also use brew install python3
  • brew list to see packages
  • brew update to update brew and its formulae
  • brew upgrade to upgrade all packages, brew upgrade $FORMULA to upgrade a specific formula

Virtualenv

Install Pip, Virtualenv, and Virtualenvwrapper:

Pip

(If you have a Mac, skip setuptools and pip; they've already been installed by Brew.)

  • sudo apt-get install python-setuptools
  • sudo easy_install pip

VirtualEnv creates a sandbox for each project where you can have a unique set of libraries. Here's a Primer on virtualenv.

In Python 3.3, it's being replaced by venv.

You do not need 'sudo' on these commands because you are using Brew. Brew installs into /usr/local, no sudo needed.

  1. Install virtualenv with pip install -U virtualenv
  2. Install virtualenvwrapper with pip install -U virtualenvwrapper
  3. cd $HOME
  4. mkdir .virtualenvs
  5. Add source /usr/local/bin/virtualenvwrapper.sh to ~/.bashrc.
  6. Check out the instructions for virtualenvwrapper
  7. Set up autoenv
  8. git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv
  9. Add this to .bashrc: source ~/.autoenv/activate.sh
$ cd myproject
$ mkvirtualenv -p /usr/local/bin/python myproject # use brew installed python
$ lssitepackages
(myproject)$ deactivate
$ cd ..
  1. If needed, you can remove a virtualenv with something like rmvirtualenv pygames.

Virtualenv with Git

This has a good explanation of what's going on. Read the comments. http://hmarr.com/2010/jan/19/making-virtualenv-play-nice-with-git/

Then, use the code on this page. Edit the .virtualenvs/postactivate and .virtualenvs/postdeactivate files. https://gist.github.com/hmarr/1013596

Virtualenv with Pip

You can use pip to quickly determine the requirements for your project with the command pip freeze > requirements.txt. The requirements.txt file will look something like this:

gnureadline==6.3.3
ipython==3.0.0
mercurial==3.3.3
pbr==0.10.8
pygame==1.9.2rc0
six==1.9.0
stevedore==1.4.0
vboxapi==1.0
virtualenv==12.1.1
virtualenv-clone==0.2.5
virtualenvwrapper==4.5.0

If you want pip to load those requirements (on another server, for example), use pip -pip install -r requirements.txt --allow-all-external

Git

My prior notes on Git

You can download git from http://git-scm.com/downloads.

You can update to the latest rev with git clone https://github.com/git/git.

Python

You can always run programs with python <program> or python3 <program>. But you can make Python programs executable from the command line with three steps:

  • chmod +x <program>
  • Make the first lie of the program #!/usr/bin/env python for Python2, #!/usr/bin/env python3 for Python3.
  • Use ./<program> to run it.

Pygame

For Mac OS

When creating the virtualenv, use the -p option to set the correct version of Python.

For Raspberry Pi

http://www.pygame.org/wiki/compileUbuntu

Python3

Robin:python(0) mikec$ which python3
/usr/local/bin/python3
Robin:python(0) mikec$ mkvirtualenv -p /usr/local/bin/python3 pygames3
...
(pygames3)Robin:python(0) mikec$ which python
/Users/mikec/.virtualenvs/pygames3/bin/python
(pygames3)Robin:python(0) mikec$ python
Python 3.4.3 (default, Apr 27 2015, 23:41:07) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'pygame'
>>> 
(pygames3)Robin:python(0) mikec$ brew install mercurial
==> Downloading http://mercurial.selenic.com/release/mercurial-3.3.3.tar.gz
######################################################################## 100.0%
...
(pygames3)Robin:python(0) mikec$ brew install git
==> Downloading https://www.kernel.org/pub/software/scm/git/git-2.3.6.tar.xz
######################################################################## 100.0%
...
(pygames3)Robin:python(0) mikec$ brew list
cmake		jpeg		mercurial	python		sdl_image	webp
freetype	libpng		openssl		python3		sdl_mixer	xz
gdbm		libtiff		pkg-config	readline	sdl_ttf
git		makedepend	portmidi	sdl		sqlite
(pygames3)Robin:python(0) mikec$ brew install sdl sdl_image sdl_mixer sdl_ttf smpeg portmidi
Error: No available formula for smpeg 
...
(pygames3)Robin:python(0) mikec$ brew tap homebrew/headonly
==> Tapping Homebrew/headonly
Cloning into '/usr/local/Library/Taps/homebrew/homebrew-headonly'...
(pygames3)Robin:python(0) mikec$ brew install --HEAD smpeg
==> Installing smpeg from homebrew/homebrew-headonly
...
(pygames3)Robin:python(0) mikec$ brew install sdl sdl_image sdl_mixer sdl_ttf smpeg portmidi
Warning: sdl-1.2.15 already installed
Warning: sdl_image-1.2.12_1 already installed
Warning: sdl_mixer-1.2.12 already installed
Warning: sdl_ttf-2.0.11 already installed
Error: smpeg is a head-only formula
Install with `brew install --HEAD smpeg`
(pygames3)Robin:python(0) mikec$ pip install hg+http://bitbucket.org/pygame/pygame
Collecting hg+http://bitbucket.org/pygame/pygame
  Cloning hg http://bitbucket.org/pygame/pygame to /var/folders/n9/6b0n40sd55g85vppq4yfyw4c0000gn/T/pip-9bne87qm-build
Installing collected packages: pygame
  Running setup.py install for pygame
Successfully installed pygame-1.9.2a0
(pygames3)Robin:python(0) mikec$ python
Python 3.4.3 (default, Apr 27 2015, 23:41:07) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>> pygame.image.get_extended()
1
>>> 
⚠️ **GitHub.com Fallback** ⚠️