| Category: Apple | |||
|---|---|---|---|
SubjectVirtual Python on macOS | |||
ContentThis document describes how to use a virtual-Python setup to layer packages of your choice on top of Python on macOS systems. You might also want to install python via Homebrew MacPorts python libraries via the Self Service application. Initial set up of Python 3.5 and upCreate a directory in the location of your choice, for these instructions we are using
Now create the virtual environment:
Setup your aliases to point to the virtual environment you have just createdTo simplify the startup process for the next time, you can add these aliases to your shell startup files, for use in subsequent shell sessions.
Once the alias is set open a new shell and type the following to setup the python virtual environment.
Now check that the shell prompt has changed to reflect the change:
.... you'll see a Python instance named which is inside your virtual environment. Installing and using Python packagesTo install the PyRAF package into the virtual environment, activate your virtual Python and then:
You can then verify that your Python package has been installed by starting Python, and saying at its prompt:
.... or (in PyRAF's case) by saying at the shell prompt:
.... and verifying that its pathname is within your virtual-Python directory. Third-party Python scriptsA script intended to be invoked by name at the shell prompt may well have the "flash-bang" line at the beginning, to tell it which (in this case) Python interpreter to use, eg one of:
This is Bad News: it hard-wires use of one particular Python interpreter (Apple's, or MacPorts's, respectively) into the program, which is likely to break portability. In third-party Python scripts or examples, you may even see:
.... or some other variant which assumes you've installed some version of Python manually. To properly generalise such scripts, edit the first line to instead say:
This will cause the script to use the first appropriate Python executable on Leaving the virtual Python environmentIf you wish (for whatever reason) to revert to using the system Python, exit from Python if necessary, then say (at the shell prompt):
Note that the shell prompt has been changed back, as saying:
.... will confirm. Cloning a virtual Python environmentVirtual Pythons have absolute pathnames stitched in, so they can't be moved or copied as-is; but they can be recreated without excessive effort. The first part is done inside the virtual Python in question using:
To add that set of packages into another virtual Python setup elsewhere, deactivate the old virtual Python, activate the new one (creating it if necesary), then saying in it:
The requirements file documents your required set of extra packages in a near-executable form. If you're keeping all your own Python programs under version control, it would be wise to add this file to your saved set as a record of any support libraries you may have found necessary, and to keep it up-to-date. Please bear in mind that the underlying system packages, which aren't mentioned here, may well differ between your desktop, your laptop, and whatever Linux servers you're using. If in doubt, use separate names for them (eg | |||
Documents | |||
| File | Heading | Date | |
| Drupal page URL | 06-12-2024 10:35 | ||
| Writer: Richard Smith Created on 01-03-2017 02:03 Last update on 22-10-2025 15:45 | 195 views This item is part of the Physics IT knowledgebase | ||