Skip to content

Latest commit

 

History

History
59 lines (42 loc) · 1.26 KB

python.md

File metadata and controls

59 lines (42 loc) · 1.26 KB

python

version management

snippets

reload classes in the interpreter

note: tested on v2.7

$ python
>>> import module.submodule
>>> foo = module.submodule.yourclass()
>>> foo.version()
v1
>>> reload(module.submodule)
>>> foo = module.submodule.yourclass()
>>> foo.version()
v2

autoload virtualenv from script

note: works on 3.8

#!/usr/bin/env python

# autoload virtualenv.
import os

directory = os.path.dirname(os.path.abspath(__file__))
if os.name == 'nt': # windows
  activate = os.path.join(directory, '../your-virtual-env-directory/Scripts/activate_this.py')
else:
  activate = os.path.join(directory, '../your-virtual-env-directory/bin/activate_this.py')
execfile(open(activate).read())

# your code here...

pretty print json

note: should word on v2.6+; tested on v2.7

echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool # directly on cli
python -m json.tool my_json.json # json in file.

force pip to use a virtualenv

export PIP_REQUIRE_VIRTUALENV=true in shell config.