Python

About

Notes

Notes

Installation

Install on Windows

Place in high-level dir (C:\PythonX.X)

Source: https://www.youtube.com/watch?v=i-MuSAwgwCU&ab_channel=IDGTECHtalk

Multiple versions

Install in different dirs

Ensure system paths are pointed to default version

Source: https://www.youtube.com/watch?v=47kQVAdA6Fc&feature=emb_logo&ab_channel=IDGTECHtalk

Install Virtual Environments & Packages

Set up project dirs, install venv to each, and install package to those venvs to preserve a clean base install.

Create New Conda Environment

Verify that anaconda is configured to PATH system environment variable.

In Anaconda CMD prompt, specify environment name and python version:

conda create -n <env_name> python=3.7

Virtual Environments: https://www.youtube.com/watch?v=ohlRbcasPAc&ab_channel=IDGTECHtalk

Packages: https://www.youtube.com/watch?v=0PUxUMZJWu4&ab_channel=IDGTECHtalk

Source: https://towardsdatascience.com/virtual-environments-104c62d48c54

Install Virtual Environment with YML File

Using Anaconda Prompt, navigate to the location where the YML file to be installed from is located.

Input the following command to create venv using the specified YML file, and name the venv:

conda env create -f yml_file.yml --name venv_name

This will create the venv in the default Anaconda venv directory:

C:\Users\<username>\anaconda\anaconda3\envs

Environment Variables

Environment Variables

NameDescription
HOME

C:\Users\username

PATH

PYTHONPATH

MAYAPATH

Package Installation

Install via PyCharm and Conda Package Manager

image

Install with Anaconda

  1. Start > Anaconda Prompt
  2. Activate relevant environment.
  3. C:\Users\username>conda activate C:/Users/<username>/anaconda/<environmentname>
  4. Check list of current packages.
  5. C:\Users\username>conda list
  6. Install package.
  7. C:\Users\username>conda install <packageName>
  8. If the package install fails due to building wheel, explore option to install via wheel downloaded from Unofficial Windows Binaries for Python Extension Packages.

Install with PIP in Virtual Environments

Verify that pip is updated to latest version.

Install via Command Prompt in Administrator Mode

System Default Python

py -m pip install package

Specific Python Version

py -2.7 -m pip install package

Install wheel file with PIP

Verify the package compatibility with Python version being downloaded and/or installed. In this example, the OpenEXR wheel file for Python 2.7 is being installed.

Activate virtual environment intended to receive package.

Note the path to the wheel file. If wheel is in the same folder as the intended Python version, then:

$ pip install OpenEXR-1.3.2-cp27-cp27m-win_amd64.whl
Processing c:\users\username\openEXR-1.3.2-cp27-cp27m-win_amd64.whl
Installing collected packages: OpenExr
Successfully installed OpenExr-1.3.2

Otherwise, include the path to the wheel file

$ pip install C:\Users\username\Documents\OpenEXR-1.3.2-cp27-cp27m-win_amd64.whl

Source: https://stackoverflow.com/questions/28568070/filename-whl-is-not-supported-wheel-on-this-platform

Packages & Modules

General

Add __init__.py files to directories so Python can treat them as packages and submodules.

__main__.py
top/
			__init__.py
			levelone/
							__init__.py
							module.py

Note the strategies for importing submodules based on the file structure above.

  • Requires full name reference
  • import top.levelone.module
    
    top.levelone.module.function()
  • Reference module only
  • from top.levelone import module
    
    module.function()
  • Reference function only
  • from top.levelone.module import function
    
    function()

References & Sources

Packages

Absolute v. Relative Imports

Modules

Load/Reload Multiple Packages

https://stackoverflow.com/questions/40353226/reload-a-whole-package-structure-in-python

https://stackoverflow.com/questions/1057431/how-to-load-all-modules-in-a-folder/1057534#1057534

UClass Type

For use of unreal.Object in Python, the uclass() decorator is used and can be supported with other decorators, like properties and functions, depending on the base object. Reference link above for docs on decorators.

Format

@unreal.uclass()
class PyObject(Object):
	python_property = unreal.uproperty(int)
	python_function = unreal.ufunction()

Example (UE4)

The use of pass notes a null block.

@unreal.uclass()
class ueUtil(unreal.GlobalEditorUtilityBase):
    pass

ueUtil().get_selected_assets()

Operation Commands

Jupyter Notebook/Lab

Activate specified virtual environment (venv_name). Navigate to base directory where Jupyter Lab is to be accessed from. This is key if the intended directory is on another drive on the computer. Once in the specified directory, input the following to access Jupyter Lab.

(venv_name) specified_directory>jupyter-lab

Bash

Check current python version and enter python mode.

$ py

List installed python versions on system.

$ py -0

Select specific python version from available system versions.

$ py -2.7

Locate path defined by environment variables. Returns the directory path.

$ echo $PYTHONPATH

Install module to a specific Python version, if multiple versions exist on system.

$ py -2.7 -m pip install -U PySide

Upgrade pip to latest version for a specific Python on system.

$ py -2.7 -m pip install --upgrade pip

List environment variables.

$ set

Anaconda Commands

List discoverable Anaconda environments

conda info --envs
conda info -e

Locate Anaconda environment

$ where anaconda
C:\Users\username\Anaconda2\Scripts\anaconda.exe

List packages in environment

conda list

Activate/Deactivate Virtual Environment

Navigate to directory where activate file is located for the intended venv, and enable it. Specified venv should be discoverable. The (env) notes that the venv is active.

$ conda activate venv_name
(env)

If venv is not accessible using the above, specify its absolute path, such as C:/Users/<username>/anaconda/pycharmCondaEnv37.

Once complete with operations within venv, verify that it is deactivated. The (env) is removed. This can also be confirmed by checking the current version of Python versus the default system version of Python.

conda deactivate

Python

List available modules in current Python version

>>> help('modules')

Information about specified module

>>> help('glob')

Import libraries accordingly.

>>> import <libName>

Exit python mode and return to bash. This can also be done with (CTRL + Z + ENTER).

>>> exit()

Output path to spec'd environment variable.

import os
print(os.environ['NUKE_PATH'])

List environment variables.

os.environ

Command Prompt (as Admin)

Associate filetype to program.

C:\windows\system32>assoc .usd=usdfiles
.usd=usdfiles
C:\windows\system32>ftype usdfiles=C:\usd\bin\usdview.cmd %1
usdfiles=C:\usd\bin\usdview.cmd %1

List environment variables.

set

List drives and uses:

net use

PowerShell

List drives and uses:

Get-PSDrive

Qt Designer

Object & Layout Coordination

image

Resources

Resources

Gists

⚠️ Public gists cannot be converted to secret.

Markdown

Writing

Writing on GitHub is supported with its markdown syntax. The following links are references:

Emoji Icons

These can quickly be accessed by typing the colon (:) button. Though not all show up immediately, others can be included by either typing additional letters to show available emoji icons or by referencing the following list of available emoji icons:

Frameworks/Modules

Libraries

Support Tools

Support Tools

Keyboard Shortcuts

PyCharm

Keyboard Shortcuts

NameShortcut

CTRL + Q

Run Cell (Jupyter Notebook)

CTRL + ENTER

Run All Cells (Jupyter Notebook

CTRL + ALT + SHIFT + ENTER

Comment Code

CTRL + /

Jupyter Notebook

Keyboard Shortcuts

NameShortcut
Access Available Functions/Methods

TAB

Access Documentation

SHIFT + TAB

QT Designer Keyboard Shortcuts

Keyboard Shortcuts

NameShortcut
Select Multiple Objects

LMB Select + ALT

Preview Form

CTRL + R

Git Resources

Resources

Python Resources

Resources