#!/usr/bin/env python # Filename: mymodule_demo.py import mymodule a = mymodule.hello("hello") a.sayhi() # print 'Version', a.version
import sys import mymodule print('command args:') for i in sys.argv: print(i) print('\n\nPython path:',sys.path,'\n') mymodule.hello()
import mymodule as m m.hello() m.show() m.sum(2, 3)
import mymodule assert (mymodule.greet("world") == "Hello, world!") print(mymodule.greet("world") == "Hello, world!") diditwork: str = mymodule.hello() print(diditwork)
import mymodule mymodule.hello("ravali")
import mymodule mymodule.hello() s = mymodule.fib(10) print(s)
# #### Import # # Import package in python is using import keyword, or from module import xx, this in C# is via using. # In[141]: import math result = math.exp(3) print(result) # In[142]: # import my own module import mymodule mymodule.hello("there") print(mymodule) # In[143]: # import object from my module from mymodule import hello hello("again haha") # In[144]: # import with alias name import mymodule as my my.hello("3rd times")
try: num = int(input("Enter your age: ")) enterage(num) except NegativeAgeException: print("Only positive integers are allowed") except: print("something is wrong") #Python Modules import mymodule print(mymodule.foo) print(mymodule.hello()) from mymodule import foo # this statement import only foo variable from mymodule print(foo) #Python *args and **kwargs def sum(a, b): print("sum is", a+b) def sum(*args): s = 0 for i in args: s += i print("sum is", s)
user_info['first_name'] = first #user_info is a dictionary user_info['last_name'] = last return user_info user_profile = build_profile('albert', 'einstein', location='princeton', field='physics') print(user_profile) #{'location': 'princeton', 'field': 'physics', 'first_name': 'albert', #'last_name': 'einstein'} #Modules should have short, all-lowercase names. import mymodule mymodule.hello() #The following lines introduce another method to use a module. from mymodule import hello as h h() #Use an alias to rename a module import mymodule as mm mm.hello() #import every function in a module. This method is discouraged. from mymodule import * hello() #import multiple functions, use comma to separate them from mymodule import hello, bye
import builtins import sys import math import numpy import mymodule from mymodule import say from mymodule import say as hello from package import package from package.left import left print('通过 module 名调用', mymodule.say()) print('通过 module 函数直接调用', say()) print('通过 module 函数别名调用', hello()) print('访问 module 全局变量', mymodule.key) print('模块导入路径', sys.path) # print('模块编译后的缓存', __pycache__) print('模块定义符号列表', dir(mymodule)) print('内置模块的函数和变量列表', dir(builtins)) print('引用包', package.package_greet()) print('引用包内子包', left.left_greet())
def print_hello(name='Masha'): print(mymodule.hello(name))
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import sys >>> sys.path ['', 'C:\\Python34\\Lib\\idlelib', 'C:\\Windows\\SYSTEM32\\python34.zip', 'C:\\Python34\\DLLs', 'C:\\Python34\\lib', 'C:\\Python34', 'C:\\Python34\\lib\\site-packages'] >>> import mymodule Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> import mymodule ImportError: No module named 'mymodule' >>> import mymodule >>> mymodule.hello() What's your name? Abdoulrazak Hello Abdoulrazak , have a nice day! >>> mymodule.hello() What's your name? Abdibogoreh Hello Abdibogoreh , have a nice day! >>>