Skip to content

Instantly share code, notes, and snippets.

@gregmark
Last active July 29, 2022 23:52
Show Gist options
  • Save gregmark/73b30c9207a492117bd8fe9d508fac5d to your computer and use it in GitHub Desktop.
Save gregmark/73b30c9207a492117bd8fe9d508fac5d to your computer and use it in GitHub Desktop.
Critical Python Tricks

Critical Python Administrative Fu

virtual env

py3org RealPy

python3 -m venv venv
source venv/bin/activate
# ...
deactivate

Special Variables

variable type meaning
sys.path list paths of available modules and packages

Print available attributes and methods

>>> print(dir())
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>> print(__name__)
__main__
>>> print(__builtins__)
<module 'builtins' (built-in)>
>>> print(type(__builtins__))
<class 'module'>
>>> print(__package__)
None
>>> print(__spec__)
None
>>> print(__doc__)
None
>>> print(__loader__)
<class '_frozen_importlib.BuiltinImporter'>
>>> print(__annotations__)
{}

Now import sys

>>> import sys
>>> content = dir(sys)
>>> print(content)
['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '__unraisablehook__', '_base_executable', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_framework', '_getframe', '_git', '_home', '_xoptions', 'abiflags', 'addaudithook', 'api_version', 'argv', 'audit', 'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth', 'getallocatedblocks', 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'last_traceback', 'last_type', 'last_value', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'platlibdir', 'prefix', 'ps1', 'ps2', 'pycache_prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'unraisablehook', 'version', 'version_info', 'warnoptions']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment