Handling of python on mac

I've been indebted to ActivePython for a long time, but since various microprojects have been completed, I wanted to manage python versions and packages more flexibly, so I moved to an environment that uses pythonbrew and virtualenv. Here, I will write the memorandum and leave it as a reminder. In addition, since everything is executed in the terminal, it does not make sense to read it if you want to do it with gui. (So, I welcome you to point out, but I did it this way, but I don't want to be complained that it didn't work.)

Postscript (2013/10/02): pythonbrew is becoming obsolete as the author announces that it will not continue development. However, the idea of pythonz, which is being developed as a successor, is different, and the specifications on mac Even after knowing this fact, I think it's worth making pythonbrew because pythonbrew is easier to use.

0. Farewell to the previous environment

I would like to mourn the Active Python that has been taken care of so far.

Based on the above page, run the following command to uninstall ActivePython.

$ sudo /Library/Frameworks/Python.framework/Versions/2.6/Resources/Scripts/uninstall

In the case of 2.7, it is OK if the 2.6 part in the middle of the path is changed to 2.7.

  1. Install pythonbrew

From here, I proceeded with reference to the following page.

Put it in crispy.

$ curl -kLO https://github.com/utahta/pythonbrew/raw/master/pythonbrew-install
$ chmod +x pythonbrew-install

Add the following line to ~ / .bashrc to get through the ./pythonbrew-install path. For csh, tcsh, and zsh people, add them to their respective rc files.

source ~/.pythonbrew/etc/bashrc

If you want to continue, reload ~ / .bashrc.

$ source ~/.bashrc

2. Installing Python with pythonbrew

We only install one version of python here, but pythonbrew allows you to install different versions of python and switch between active python versions. Please refer to it if you are interested because it is listed on the link page.

$ pythonbrew install 2.7.2 --framework
$ pythonbrew switch 2.7.2
$ sudo xcodebuild -license

3. Introduction of virtualenv

virtualenv is software that can virtually prepare an environment such as a group of packages when executing python.

It may not come as a pinch, but since python version control and package dependency resolution are relatively rough, it works with this version combination of this package, but it does not work elsewhere. Sometimes there is. It is indispensable for heavy users because it is irresistible to reinstall the package every time.

$ pip install virtualenv
$ pip install virtualenvwrapper

4. Practical edition

That's it, but finally, let's take a look at a simple way to use virtualenv.

$ virtualenv sandbox
$ cd sandbox
$ source bin/activate
(sandbox) $ pip install numpy
(sandbox) $ python
Python 2.7.2 (default, Sep 26 2013, 15:18:47) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy

(sandbox) $ deactivate
$ python
>>> import numpy
Traceback (most recent call last):File "<stdin>", line 1, in <module>ImportError: No module named numpy

In the above, a virtual environment is built in a directory called sandbox. To use the built virtual environment, read the file bin / activate in the virtual environment (with source). After reading, it will be in a state with the prefix (environment name). The package installed in this state is installed in the virtual environment, but it is not installed in the environment returned by the command deactivate that can be used in the virtual environment.

By using different virtual environments in this way, more flexible software development becomes possible. If you are a heavy python user, please do. It is also useful for checking dependencies when testing software.

That said, I think most heavy users are using it, so I'm the only one who's late ...

Recommended Posts

Handling of python on mac
python on mac
Environment construction of python3.8 on mac
Install Python on Mac
Install Python 3.4 on Mac
Install pygame on python3.4 on mac
python> Handling of 2D arrays
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]
Notes on installing Python on Mac
Building a Python environment on Mac
If python on mac goes missing
Create a Python environment on Mac (2017/4)
Python environment construction memo on Mac
Install Python 3.7 Anaconda on MAC, but Python 2
Handling of JSON files in Python
Install python3 on Mac (El Capitan)
If Python 3.5.0 installation fails on Mac
Steps to install python3 on mac
Call C / C ++ from Python on Mac
Update Python on Mac from 2 to 3
Notes on handling large amounts of data with python + pandas
Easy on Mac! Plot of unit step response using Python
Introduction of Python
Python on Windows
twitter on python3
Introducing MAMP + Python + SQLAlchemy (Mysql-Python) on Mac
Create a python environment on your Mac
Follow active applications on Mac with Python
Python Error Handling
Basics of Python ①
Basics of python ①
How to erase Python 2.x on Mac.
Python exception handling
Notes on building Python and pyenv on Mac
Build Python environment with Anaconda on Mac
Copy of python
Python timezone handling
Study on Tokyo Rent Using Python (3-1 of 3)
Python on Windbg
Video processing using Python + OpenCV on Mac
[Python] Japanese localization of matplotlib on Ubuntu
Python exception handling
Introduction of Python
I tried putting various versions of Python + OpenCV + FFmpeg environment on Mac
A memo of a tutorial on running python on heroku
GPU ~ Implementation of PlaidML on Mac ~ (as of May 2020)
Build a Python environment on Mac (Mountain Lion)
Run Zookeeper x python (kazoo) on Mac OS X
Build a Python development environment on your Mac
Try working with Mongo in Python on Mac
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
At the time of python update on ubuntu
[Python] Chapter 02-02 Basics of Python programs (Handling of character strings)
Shpinx (Python documentation builder) on Mac OS X
[Data science memorandum] Handling of missing values ​​[python]
Try importing MLB data on Mac and Python
Install Tensorflow on Mac
Python conda on cygwin
[Python] Operation of enumerate
List of python modules