So I decided to try to install (and virtualenv) on my Mac Book Pro. Virtualenv was explained to me as being python’s version of Wayne E. Seguin’s RVM (Ruby Version Manager). I’ve tried installing virtualenv/wrapper twice before and failed miserably. But this time I decided to actually do it.
Some quick background: I’ve been a very active python programmer up to and including 2.5 series. I’ve implemented large python-based systems at several of the companies I’ve worked for; including two very successful web applications.
Using virtualenv, I run my projects with the default version of Python (2.7). On one project, I need to use Python 3.4. I used brew install python3 to install it on my Mac. The preinstalled version of Python on Mac OS X is not ideal for development. Therefore, you should install a version of Python that is better suited for development. Install Python on Mac OS X; Step-by-step instructions. Open a terminal and install the following packages. Pip install virtualenv virtualenvwrapper Setup virtualenvwrapper in.
I’ve implemented two ORMs in python and even grokked meta-programming in python. But that was several years ago. I’ve not really done much programming in python since 2007 or so. Except for some smaller utilities. I’ve since started working with Ruby, heavily in the last year, and am really liking it.
It took me a while to understand what the underlying philosophy was about but I get it now. Other than some ugly warts (threading in 1.9 acts differently on different OSes and a lot of base classes/types are privileged and don’t deal well with being overridden) Ruby is pretty spiffy. Since I’ve last done serious programming in python, it has gotten easyinstall (it existed, but was beta), pip, and the.egg package format. All big improvements. Anyway, back to virtualenv/wrapper.
The biggest hurdle the first times I tried installing (and this time too) was that I didn’t understand what virtualenv and virtualenvwrapper are. For those who don’t know: Virtualenv does not install new python versions.
You need the versions installed some other way (such as a package manager or compiling them yourself). It is a way to manage python packages and libraries. If you’re familiar with RVM, then it’s basically a way to do rvm gemset for python. However, virtualenv is clunky to use allegedly, I’ve not played with it. Fortunately, we have virtualenvwrapper to make life easier! This is similar to RVM in the way it is designed — it’s a bunch of shell functions you source into your shell.
You then get commands like: mkvirtualenv Creates a new virtual environment (a python gemset, if you will). Rmvirtualenv Three guesses and the first two don’t count.
Workon Sets which environment to work on. If you don’t specify one, then it’ll list them all. Sort of like rvm use. Deactivate Deactivate your python environments. Sort of the equivalent of rvm use system. The instructions So here’s how I installed virtualenv/wrapper on OS-X: Install X Code is the developer tools for Mac OS X. It includes the classic tools like gcc, make, etc.
As well as some spiffy Apple designed tools. It’s only $4.99 and worth the price if you’re going to do any development on your Mac. Warning: The download is huge. Install homebrew I installed.
Actually, I’ve had it for while, but if you’re following this on a new Mac, you might want to know that I had installed it. The instructions are straightforward so I won’t repeat them here. Install python 2.7 This is optional, I suspect. I didn’t play with installing virtualenv/wrapper on the system’s python. I try to leave the system stuff alone, if possible.
It saves me headaches later. Especially if I mess something and need to try again. Brew install readline sqlite gdbm brew install python -universal I do this in two steps because:. I wanted to ensure the dependencies for python are installed before python.
Homebrew is getting better with dependencies but it isn’t perfect. If they aren’t installed before, then you would have to uninstall and re-install python to get python notice them.
I want to make python —universal so I can build 32bit and 64bit versions. Install pip So this was something that confused me greatly, but I eventually figured it out. You need to run easyinstall, but I couldn’t figure out where easyinstall was in Homebrew. I looked all over and didn’t find it. Fortunately, I got frustrated and tried uninstalling and re-installing python and sow this message at the end of the python install. A 'distutils.cfg' has been written, specifing the install-scripts folder as: /usr/local/share/python If you install Python packages via 'python setup.py install', easyinstall, pip, any provided scripts will go into the install-scripts folder above, so you may want to add it to your PATH. Distribute has been installed, so easyinstall is available.
To update distribute itself outside of Homebrew: /usr/local/share/python/easyinstall pip /usr/local/share/python/pip install -upgrade distribute Yay! Now I know where easyinstalland hey, those are the instructions for installing pip! # Setting up the VirtualEnv export WORKONHOME = $HOME/.virtualenvs export VIRTUALENVWRAPPERPYTHON =/usr/local/bin/python2.7 export VIRTUALENVWRAPPERVIRTUALENVARGS = '-no-site-packages' export PIPVIRTUALENVBASE = $WORKONHOME export PIPRESPECTVIRTUALENV =true if -r /usr/local/share/python/virtualenvwrapper.sh ; then source /usr/local/share/python/virtualenvwrapper.sh else echo 'WARNING: Can't find virtualenvwrapper.sh' fi This does a couple of things:. It sets the home for virtual envs. Uses python2.7 by default.
Tells it not to install site-packages. I don’t plan on installing any into homebrew’s python, but just in case. Tells pip to honor the virtualenv stuff. The last bit was the part I banged my head on the most, even though I didn’t know it. I was following some instructions on installing virtualenvwrappers and they left that part out.
Commands like mkvirtualenv are shell functions. Virtualenvwrapper.sh contains those functions. I kept looking for shell scripts or programs or something.
I put a conditional and a warning around it incase I change homebrew or uninstall something. Conclusion So hopefully someone finds this useful.
If not, I’ll refer back to it at some point in the future when I need to redo all this for some reason. I’ll have forgotten it all by then.
Python Development Environment on macOS High Sierra Last updated: July 22, 2018 While installing Python and Virtualenv on macOS High Sierra can be done several ways, this tutorial will guide you through the process of configuring a stock Mac system into a solid Python development environment. First steps This guide assumes that you have already installed Homebrew. For details, please follow the steps in the. Python We are going to install the latest version of Python via Homebrew. Why bother, you ask, when Apple includes Python along with macOS?
Here are some reasons:. When using the bundled Python, macOS updates can nuke your Python packages, forcing you to re-install them. As new versions of Python are released, the Python bundled with macOS will become out-of-date. Homebrew always has the most recent version. Apple has made significant changes to its bundled Python, potentially resulting in hidden bugs.
Homebrew’s Python includes the latest versions of and (Python package management tools) Use the following command to install Python 3.x via Homebrew. Hg debuginstall If the last line in the response is “no problems detected”, then Mercurial has been installed and configured properly. Virtualenv Python packages installed via the steps above are global in the sense that they are available across all of your projects. That can be convenient at times, but it can also create problems. For example, sometimes one project needs the latest version of Django, while another project needs an older Django version to retain compatibility with a critical third-party extension.
This is one of many use cases that was designed to solve. On my systems, only a handful of general-purpose Python packages (such as and Virtualenv are globally available — every other package is confined to virtual environments. With that explanation behind us, let’s install Virtualenv.
Virtualenv -p python3 foobar-py3 which makes it easier to switch between Python 2.x and 3.x foobar environments. Restricting Pip to virtual environments What happens if we think we are working in an active virtual environment, but there actually is no virtual environment active, and we install something via pip3 install foobar? Well, in that case the foobar package gets installed into our global site-packages, defeating the purpose of our virtual environment isolation. In an effort to avoid mistakenly Pip-installing a project-specific package into my global site-packages, I previously used easyinstall for global packages and the virtualenv-bundled Pip for installing packages into virtual environments.
That accomplished the isolation objective, since Pip was only available from within virtual environments, making it impossible for me to pip3 install foobar into my global site-packages by mistake. But easyinstall has some deficiencies, such as the inability to uninstall a package, and I found myself wanting to use Pip for both global and virtual environment packages. Thankfully, Pip has an setting that tells it to bail out if there is no active virtual environment, which is exactly what I want. In fact, we’ve already set that above, via the require-virtualenv = true directive in Pip’s configuration file. For example, let’s see what happens when we try to install a package in the absence of an activated virtual environment.