search
HomeBackend DevelopmentPython Tutorial在Mac OS上搭建Python的开发环境

一. 安装python

mac系统其实自带了一个python的执行执行环境,用来运行python还行,但是开发可能就不够了,因此我们需要重新安装python。这里有两种方案安装:

1.homebrew

brew install python

这个方案比较简单,如果出错的话可以给前面加sudo试试,这个安装的python可能不是最新版.

2.从官网下载安装
大家可以从https://www.python.org/download下载安装最新版的python,安装比较无脑,一路按下去就OK,缺点是以后升级,卸载都得自己维护.

这两个方法安装的python的位置是不一样的,大家可以用:

which python

来查看安装位置.安装完成后在终端中键入python来验证安装是否成功.

二. 安装pip

这里好多文章中说要先安装easy_install, 其实是不用的.

1.我们先获取pip安装脚本:

wget https://bootstrap.pypa.io/get-pip.py

如果没有安装wget可以去这里将所有内容复制下来,新建get-pip.py文件,将内容拷进去就OK了.

2.安装pip

sudo python get-pip.py

用python执行刚才获取的脚本,这里sudo可以选择使用,若遇到类似这个报错则必须加sudo:

Exception:

Traceback (most recent call last):

...

OSError: [Errno 13] Permission denied: 'XXX/pip-0.7.2-py2.7.egg/EGG-INFO/dependency_links.txt'

Storing debug log for failure in /Users/bilt/.pip/pip.log

安装成功后可以在终端中键入pip来检测,如果不行重启终端后尝试.

3.修改pip源

在天朝,由于功夫网的原因,使用pip安装一些模块会特别慢甚至无法下载,因此我们需要修改pip的源到国内的一些镜像地址,特别感谢国内无私奉献的组织~

首先进入HOME路径:

cd ~

创建.pip目录:

mkdir .pip

创建pip.conf文件:

touch pip.conf

大家可以用自己喜欢的编辑器打开pip.conf文件,我现在使用的时v2ex的源,所以添加:

[global]
index-url = http://pypi.v2ex.com/simple

大家可以把index-url的值设置为自己实际源的地址.

至此pip源修改成功,以后使用pip安装模块时都会从这个源去下载安装,大家可以自行测试一下.

三. 其他模块安装

1.Pillow/PIL

想用python处理图片,自然少不了PIL这个模块, 由于PIL长期没有更新了, 所以有了Pillow这个模块, 依赖于PIL, 新版的pip安装后会自带Pillow, 但是好像没有zlib模块, 所以会报错:

File "/Library/Python/2.7/site-packages/PIL/Image.py", line 1105, in paste
 im.load()

 File "/Library/Python/2.7/site-packages/PIL/ImageFile.py", line 190, in load

 d = Image._getdecoder(self.mode, d, a, self.decoderconfig)

 File "/Library/Python/2.7/site-packages/PIL/Image.py", line 389, in _getdecoder

 raise IOError("decoder %s not available" % decoder_name)

IOError: decoder zip not available

因此我们需要手动重新安装:

sudo pip install -U Pillow

2.MySQLdb
在下面的网址下载mysqldb模块:

http://sourceforge.net/projects/mysql-python/
在mac os x直接双击解压,命令行进入解压后的目录, 执行python setup.py build

如果有

sh: mysql_config: command not found

提示,我们需要编辑下mysql的路径,使用vim打开setup_posix.py

找到:

mysql_config.path = "mysql_config"

改为:

mysql_config.path = "/usr/local/mysql/bin/mysql_config"

然后执行:

sudo python setup.py install

安装成功后,在命令行输入python进入python环境,输入import MySQLdb,我的环境中报下面的错误:

>>> import MySQLdb
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "MySQLdb/__init__.py", line 19, in </module><module>
 import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
 Referenced from: /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.8-intel.egg/_mysql.so
 Reason: image not found

解决方法,我们建立一个软链就可以了

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

这样我们就在mac os x的python环境下安装好了MySQLdb模块

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.