In startup.py:
import worker
if __name__ == '__main__':
# this block executes if we run startup.py directly but
# not if this module is imported by another
worker.run()
In worker.py:
import time
def run():
while 1:
print "I'm running!"
time.sleep(1)
Packages are collections of modules.
myproject |- main.py |- worker.py |- widgets |- __init__.py |- button.py |- listbox.py |- combobox.py
In main.py:
import widgets
import worker
class MyApp(object):
def __init__(self):
self.combo = widgets.Combobox(['NC', 'SC', 'GA', 'VA'], 'Search')
def run(self):
worker.run()
if __name__ == '__main__':
app = MyApp()
app.run()
In widgets/__init__.py:
from .button import Button
from .listbox import Listbox
from .combobox import Combobox
In widgets/combobox.py:
from .button import Button
from .listbox import Listbox
class Combobox(object):
def __init__(self, items, button_label='Submit'):
self.listbox = Listbox(items)
self.button = Button(button_label)
# import math module, assign to variable 'math' in this
# module's global namespace
import math
# import math module, assign to variable 'm' in this
# module's global namespace
import math as m
# import 'sin' and 'cos' from module math
# assign to 'sin' and 'cos' variables
from math import sin, cos
# import everything from math and assign to vars in this
# module's global namespace; typically considered bad form
from math import *
These are only possible within a package.
# import 'parse' from a peer module named 'minidom' within
# the same package as this module
from .minidom import parse
# import everything from 'etree', a peer of this module's package;
# using * for relative imports within your own package structure
# is more acceptable than absolute * imports
from ..etree import *
"A tool for installing Python packages."
- pip Homepage
Commands to master:
$ pip search $ pip install $ pip uninstall
search, install, uninstall
ipython==1.1.0 tornado==3.1.1 Jinja2==2.7.1 pyzmq==14.0.1 numpy==1.8.0 pandas>=0.12.0
To process this file we run:
$ pip install -r requirements.txt
"virtualenv is a tool to create isolated Python environments."
- virtualenv Introduction
Commands to master:
$ virtualenv [--no-site-packages] <name> $ <name>/bin/activate (name)$ deactivate
virtualenv, activate, pip, deactivate