Install a Python package

To install a Python package on Macintosh

  1. Download the Python package to your workstation,
  2. Unpack the package,
  3. Open a terminal window,
  4. Start sudo,
    sudo -i
  5. Change to the directory of the package,
  6. Install the package.
    pip install PACKAGE_NAME

If you don’t install the package in sudo mode, you will get “Permission denied” errors during the package installation.

 

PyCharm configuration

When you install PyCharm, the default Python interpreter is may not the version you want to use for your new projects. When you create a new project, and the project interpreter is not set, you may get the following error message:

Executed command:
/var/folders/_x/_f0zzs1s4sx6bk4s8n78l2jsbn9r3z/T/tmpBoQLx_pycharm-management/pip-9.0.1/setup.py install

Error occurred:
40:357: execution error: /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils/dist.py:266: UserWarning: Unknown distribution option: ‘python_requires’

Command output:
40:357: execution error: /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils/dist.py:266: UserWarning: Unknown distribution option: ‘python_requires’
warnings.warn(msg)
warning: build_py: byte-compiling is disabled, skipping.
warning: install_lib: byte-compiling is disabled, skipping.
error: byte-compiling is disabled.
(1)

Solution:

Set the default Python interpreter to the latest installed version before you create your project:

  1. Start PyCharm
  2. In the File menu select Default Settings
  3. On the left side select Project Interpreter,
  4. On the right side click the arrow of the Project Interpreter drop-down
  5. Select the Python interpreter you want to use by default, and click the OK button.

Python ImportError: No module named …

Symptom:

from MY_FOLDER.MY_FILE import MY_CLASS causes an error:
ImportError: No module named MY_FOLDER.MY_FILE

Debugging:

  1. Execute the program in debug mode,
  2. In the Variables window expand Special Variables,
  3. Read the __warningregistry__ value.

Possible cause:

The __init__.py file is missing in the directory of the referenced class.

Solution:

Create an empty __init__.py file in the directory where the class is referenced from.

Working with PyCharm

Project management

Create a new project in PyCharm

  1. In the File menu select New Project…,
  2. On the left side select the Python framework you want to use,
  3. Enter the name for your project to the end of the Location path,
  4. If you have selected a framework that asks for more information
    1. Click the More Settings link and enter the additional values,
    2. Click the Create button,
    3. PyCharm generates folders for your project and application,
    4. When PyCharm asks you about compatibility inspection, enable it, to make sure your code is compatible with future versions of Python.
    5. PyCharm will enable Code compatibility inspection. Click OK to save the setting.
    6. PyCharm will still set your project interpreter to 2.6, so set it to 2.7. In the PyCharm menu select Preferences…,
    7. On the left side select Project:…, Project Interpreter,
    8. In the Project Interpreter drop-down select 2.7 and click the OK button.

Delete a project in PyCharm

The PyCharm project explorer does not have a delete option. To delete a PyCharm project

  1. Open the project location in Finder,
  2. Move the project folder to the Trash,
  3. The PyCharm project explorer window will reflect the change.

Shortcuts, tips, and tricks

 Auto generate

Auto generate functions, classes, attributes

  1. Enter the name of the undefined element where you want to use it,
  2. Click the light bulb, or press ⌥⏎ (Alt-Enter) to pop up the Intention Action alert,
  3. Select the action to generate the definition.

Auto generate fields for classes

  1. Enter the __init__ function of the class with the field names,
  2. Place the cursor on the field you want to generate the definition for,
  3. Click the light bulb, or press ⌥⏎ (Alt-Enter) to pop up the Intention Action alert,
  4. Select Add field ‘…’ to class …,
  5. Press Enter to select the option,
  6. Press Enter at the end of the new line to move the cursor to the next line, otherwise, the Intention Action alert does not pop up when you press ⌥⏎ (Alt-Enter)

 

Code completion

  • Press Ctrl-Space to complete the name of the element.
  • Press Ctrl-Space twice to complete the name of any class, even if it is not yet imported. The import statement will be automatically added.

Quick documentation

To get a short documentation on an element under the cursor, press F1. (By default on a Macintosh you need to press fn-F1, because the F1 button is programmed to adjust the brightness.)

For more information

Auto completing code, see https://www.jetbrains.com/help/pycharm/auto-completing-code.html

Keyboard shortcuts
https://www.jetbrains.com/help/pycharm/keyboard-shortcuts-and-mouse-reference.html