Python dev mac os

5. Using Python on a MacВ¶

Python on a Mac running macOS is in principle very similar to Python on any other Unix platform, but there are a number of additional features such as the IDE and the Package Manager that are worth pointing out.

5.1. Getting and Installing MacPythonВ¶

macOS since version 10.8 comes with Python 2.7 pre-installed by Apple. If you wish, you are invited to install the most recent version of Python 3 from the Python website (https://www.python.org). A current “universal binary” build of Python, which runs natively on the Mac’s new Intel and legacy PPC CPU’s, is available there.

What you get after installing is a number of things:

A Python 3.9 folder in your Applications folder. In here you find IDLE, the development environment that is a standard part of official Python distributions; and PythonLauncher, which handles double-clicking Python scripts from the Finder.

A framework /Library/Frameworks/Python.framework , which includes the Python executable and libraries. The installer adds this location to your shell path. To uninstall MacPython, you can simply remove these three things. A symlink to the Python executable is placed in /usr/local/bin/.

The Apple-provided build of Python is installed in /System/Library/Frameworks/Python.framework and /usr/bin/python , respectively. You should never modify or delete these, as they are Apple-controlled and are used by Apple- or third-party software. Remember that if you choose to install a newer Python version from python.org, you will have two different but functional Python installations on your computer, so it will be important that your paths and usages are consistent with what you want to do.

IDLE includes a help menu that allows you to access Python documentation. If you are completely new to Python you should start reading the tutorial introduction in that document.

If you are familiar with Python on other Unix platforms you should read the section on running Python scripts from the Unix shell.

5.1.1. How to run a Python scriptВ¶

Your best way to get started with Python on macOS is through the IDLE integrated development environment, see section The IDE and use the Help menu when the IDE is running.

If you want to run Python scripts from the Terminal window command line or from the Finder you first need an editor to create your script. macOS comes with a number of standard Unix command line editors, vim and emacs among them. If you want a more Mac-like editor, BBEdit or TextWrangler from Bare Bones Software (see http://www.barebones.com/products/bbedit/index.html) are good choices, as is TextMate (see https://macromates.com/). Other editors include Gvim (http://macvim-dev.github.io/macvim/) and Aquamacs (http://aquamacs.org/).

To run your script from the Terminal window you must make sure that /usr/local/bin is in your shell search path.

To run your script from the Finder you have two options:

Drag it to PythonLauncher

Select PythonLauncher as the default application to open your script (or any .py script) through the finder Info window and double-click it. PythonLauncher has various preferences to control how your script is launched. Option-dragging allows you to change these for one invocation, or use its Preferences menu to change things globally.

5.1.2. Running scripts with a GUIВ¶

With older versions of Python, there is one macOS quirk that you need to be aware of: programs that talk to the Aqua window manager (in other words, anything that has a GUI) need to be run in a special way. Use pythonw instead of python to start such scripts.

With Python 3.9, you can use either python or pythonw.

5.1.3. ConfigurationВ¶

Python on macOS honors all standard Unix environment variables such as PYTHONPATH , but setting these variables for programs started from the Finder is non-standard as the Finder does not read your .profile or .cshrc at startup. You need to create a file

/.MacOSX/environment.plist . See Apple’s Technical Document QA1067 for details.

For more information on installation Python packages in MacPython, see section Installing Additional Python Packages .

5.2. The IDEВ¶

MacPython ships with the standard IDLE development environment. A good introduction to using IDLE can be found at http://www.hashcollision.org/hkn/python/idle_intro/index.html.

5.3. Installing Additional Python PackagesВ¶

There are several methods to install additional Python packages:

Packages can be installed via the standard Python distutils mode ( python setup.py install ).

Many packages can also be installed via the setuptools extension or pip wrapper, see https://pip.pypa.io/.

5.4. GUI Programming on the MacВ¶

There are several options for building GUI applications on the Mac with Python.

PyObjC is a Python binding to Apple’s Objective-C/Cocoa framework, which is the foundation of most modern Mac development. Information on PyObjC is available from https://pypi.org/project/pyobjc/.

Читайте также:  Скрыть группу пользователей windows

The standard Python GUI toolkit is tkinter , based on the cross-platform Tk toolkit (https://www.tcl.tk). An Aqua-native version of Tk is bundled with OS X by Apple, and the latest version can be downloaded and installed from https://www.activestate.com; it can also be built from source.

wxPython is another popular cross-platform GUI toolkit that runs natively on macOS. Packages and documentation are available from https://www.wxpython.org.

PyQt is another popular cross-platform GUI toolkit that runs natively on macOS. More information can be found at https://riverbankcomputing.com/software/pyqt/intro.

5.5. Distributing Python Applications on the MacВ¶

The standard tool for deploying standalone Python applications on the Mac is py2app. More information on installing and using py2app can be found at http://undefined.org/python/#py2app.

5.6. Other ResourcesВ¶

The MacPython mailing list is an excellent support resource for Python users and developers on the Mac:

Another useful resource is the MacPython wiki:

Источник

Python dev mac os

Mac Setup for Web Development Intensive

This is Bitmaker’s guide to setting up a development environment on a Mac for the Web Development Intensive course. If you are using Linux (or need to install Linux in place of Windows), please refer to this guide instead.

Linux and Mac are required for this course. Using Windows for the course is not recommended at all. Note that the instructions for Linux will be different from what’s described here.

This process should take about 1 hour to 1.5 hours to complete, depending on the speed of your machine and your internet connection.

This guide is written assuming you are running OS X El Capitan 10.11 or later. These instructions may still work for OS X 10.9 or 10.10, but is not guaranteed. You should upgrade to 10.11 before proceeding. We’ll be waiting for you right here.

If you’re a new developer and you haven’t installed any development tools before, you can skip the next two paragraphs and go straight to the next step: XCode and Command Line Tools.

This guide assumes that you’re using bash shell, which is the default shell for the OS X Terminal. We also assume that you use .bash_profile to setup $PATH and other environment variables. If you use a different bash config file, be sure to substitute it where appropriate below.

XCode and Command Line Tools

We need to install the tools that allow us to compile programs specifically for your machine. These are provided by Apple and are pretty easy to install.

Open the Terminal program. You can find it in the Other folder in Launchpad.

Enter the following command on the terminal command line.

It will pop up a dialog box like the following.

Click Install to proceed. It is going to take a few minutes to complete.

To prove that it successfully installed, run xcode-select -p . It should show the directory where Xcode is installed.

By default, when you view a listing of your files with the ls command, hidden files won’t show up (i.e. files beginning with a ‘.’), nor will the details about each file appear. More often than not, we want to see those details, so let’s alias ls to show these details everytime we use ls .

Enter the following from within your terminal:

Next we’ll install Homebrew, a package manager which we’ll use to install most of our other required command-line tools. It’s a much more convenient alternative to compiling the code ourselves from source.

To install Homebrew, copy, paste and run the following at the command line:

You’ll have to enter your Mac password to install Homebrew.

Once it’s installed, run:

to make sure Homebrew installed correctly.

If brew doctor says Your system is ready to brew , then everything worked properly and you can skip the following step «Your system is not ready to brew» and proceed straight to Try out Homebrew. Lucky you.

Your system is not ready to brew

You might see something like this:

This is a problem. Let’s fix this by moving the bin directory that Homebrew sets up for us ahead of every other folder specified in PATH. Run the following:

This will create a .bash_profile config file which is read and executed each time a new terminal is opened. To apply changes made to this file, you can either restart terminal (ghetto mode), or run source

If you see other issues, try reading the instructions carefully and doing what they suggest. If brew doctor continues to issue warnings you can contact an instructor for help. You can also post a question in the #askusanything channel on Slack.

Try out Homebrew

Try Homebrew out by installing wget :

In the future, run brew update to get the latest Homebrew formulas, and brew upgrade to update to the latest versions of installed applications.

Читайте также:  Файлы по сети windows linux

You don’t need to run these commands right now because you’ve already just installed the latest and greatest!

An Aside: Why PATH Order is Important

Command-line executables are searched by going through each folder in the $PATH variable, one by one in the order listed. As soon as an app with the same name is found, it stops searching the rest of the folders. OS X comes with built-in apps (and you might have your own apps installed prior to this), but we often want to use newer versions instead. To make sure the newer version gets ‘picked up’, we need to ensure that the symlinked Homebrew /bin folder comes before other system folders. To see the PATH directories, run

Homebrew packages are downloaded and installed in /usr/local/Cellar/ by default, and symlinked into /usr/local/bin . This folder will not be overridden the next time Apple releases an incremental OS X update.

If you’re a new developer and this section didn’t make much sense to you, don’t worry about it; you’ll pick it up in the fullness of time.

Food for Thought

  • How do I get a list of Homebrew packages that are installable?
  • How do I get a list of currently installed Homebrew packages?
  • How do I update an existing package?

(You’ll probably have to read the Homebrew documentation to answer these questions.)

VS Code Text Editor

Install VS Code

Download VS Code. Remember to drag the app from your Downloads folder into your Applications folder to install it into your system. Double click to launch it and take a look around.

VS Code is the text editor we recommend for the course. Other alternatives are Atom, Sublime Text 3 and Textmate 2.

Now that you’ve installed VS Code, you might also want to set it as your default system editor by running the following command from any directory in your terminal:

We may need to manage multiple versions of Python during the course. Run the following in your terminal:

Additionally, run the following to ensure your command line works nicely with pyenv:

/.bash_profile echo ‘export PATH=»$PYENV_ROOT/bin:$PATH»‘ >>

/.bash_profile echo ‘eval «$(pyenv init -)»‘ >>

List the possible Python installations:

Choose the latest version. Scroll up — it should just be a version number without any prefix or suffix. Eg. 3.7.1 , not 3.7.1-dev or pypy3.5-6.0.0 .

To check that you’ll be able to install new Python packages using pip, run:

If you see a version number, you’re good to go!

Virtualenv is a tool that allows each project you create to separately manage its Python dependencies. We’ll be using it throughout the course.

Lastly, to get pyenv , which manages our Python versions, working nicely with virtualenv :

/.bash_profile echo ‘eval «$(pyenv virtualenv-init -)»‘ >>

Let’s test that everything works together!

PostgreSQL is awesome. Prefer it over MySQL if you have the choice.

The easiest way to install this is via Postgres.app. Download it, drag it to the applications folder, and then double-click to launch. While it’s open, the database server is running (it adds an elephant icon to your taskbar). When you close it, the database server shuts down.

Postgres Command-Line Tools

Install the Postgres command-line tools as follows:

Close and reopen Terminal, and then run:

It should say something like:

You need to have Django installed in order to create new Django projects. After the project is created (or if you’re working with an existing Django project), you’ll be using the bundled versions of Django specific to your project.

Never run sudo in front of these pip commands, or it may install to the wrong folder.

Django might take a while to install. When the installation is done, you can look at your installed Python modules by running

Verify that Django is there and that the version is at least 2.1.4 . Run:

It should say (at least) 2.1.4 .

Making a New Django Project

You should create a directory where you can put all of your work. Run:

This will create a work directory inside the OS X Documents folder. The words ‘directory’ and ‘folder’ are interchangeable. ‘Folder’ is the word generally used by non-technical people and ‘directory’ is the word generally used by technical people. Now that you are being initiated as techies, we’ll use the ‘directory’ term!

symbol refers to your Home Directory. You should put all of your work for this course inside the work directory, or some other directory of your choosing if you have another preference.

Go inside your work directory:

and then make a new Django project:

This step will probably take a few minutes the first time you create a new Django project. The next time, it’ll run much faster.

Читайте также:  Mac os что такое dawn

Running a Django project

Next, go into your new project directory

Visit http://localhost:8000 in your browser. If you see the The install worked successfully! Congratulations! page, congrats – Django works!

You can type ctrl + c into your terminal to stop the Django application.

In this course, you’re going to be running the python manage.py runserver and ctrl + c commands very, very often, so go ahead and memorize them now!

Editing a Django project

Finally, you can open up the project files in VS Code with the following command:

The . symbol means ‘this directory’, so the command means ‘open VS Code in this directory’.

Poke around the project files as much as you like. Soon we’ll be learning all about what makes a Django app tick. Isn’t it exciting?

Now, close the editor and continue to the next step.

Delete the new Django project

To keep your work directory clean, let’s delete the new project you just created.

You are currently in your project directory. Check that by running:

It should say something like /Users/username/Documents/work/my_awesome_app .

Let’s go back up one directory, to your work directory.

Double check where you are:

It should say something like /Users/username/Documents/work .

Double check the contents of the directory

If you’ve followed all the directions so far, there should only be single item called my_awesome_app . Go ahead and delete this project, it has served its purpose.

Be careful running this rm -r command! It’s a command you need to learn, but always double and triple check what you’re deleting. More information on the rm -r command

People can (and have) deleted their entire computers by misusing rm -r . Be extra careful! It’s one of the most dangerous commands you can run.

Register on Github

First you’ll need to setup your GitHub account.

Go to github.com and register a free account with your usual email address.

git comes with OS X but it’s typically an older version. Let’s get a newer one.

It should say something like git version 2.9.2 . The version number should be >= 2.9.

Next, tell Git your name so that your commits will be properly labelled. Substitute your actual name for YOUR NAME , of course.

Now tell Git the email address that will be associated with your Git commits. This email address should be the same one that you registered with Github. Subtitute it for YOUR EMAIL ADDRESS below.

Generate SSH Keys

SSH keys are definitely a better setup to use with Git than HTTPS. This is so you don’t have to type your password every time you push. Don’t worry if these terms don’t mean anything to you right now, you’ll eventually learn what they are during your journey as a developer.

First let’s make sure you don’t already have existing SSH keys on your computer.

It should say something like ls: /Users/username/.ssh: No such file or directory .

With Terminal still open, run the following. Be sure to subtitute your Github email address.

When you are prompted to Enter file in which to save the key , just press Enter to continue and it will use the default filename id_rsa .

You’ll then be asked to Enter passphrase (empty for no passphrase) . Enter a very good, secure passphrase (be sure that it’s something you can remember).

Go ahead and memorize that passphrase now because it’ll be needed again soon, in the Test the Connection step.

Next you’ll be asked to Enter same passphrase again . Do so. You sould then see something like:

And it means we can move onto the next step.

Add your SSH Key to your Github Account

Copy the SSH key to your clipboard.

Follow these steps to add the copied key to your Github account.

  • In the top right corner of Github, click your profile photo, then click Settings.
  • In the user settings sidebar, click SSH keys.
  • Click Add SSH key.
  • In the Title field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key «Personal MacBook Air».
  • Paste your key into the Key field. ( ctrl-v )
  • Click Add key.

Test the Connection

Open Terminal and enter:

You will see something like:

Type yes and then press Enter.

Next, a window like the following should pop up:

Make sure the Remember password in my keychain box is checked.

Then type in the passphrase that you used to generate the SSH Key, and press OK.

If you see the following, then your Github account has been set up properly!

If you receive a message about «access denied,» please see an instructor for help.

Whew, you’re done installing a working Django development environment! Now take a break, you deserve it!

Источник

Оцените статью