Python Modules






<!--
main_leaderboard, all: [728,90][970,90][320,50][468,60]
-->



Python Modules



❮ Previous
Next ❯



What is a Module?


Consider a module to be the same as a code library.


A file containing a set of functions you want to include in your application.




Create a Module


To create a module just save the code you want in a file with the file extension .py:




Example


Save this code in a file named mymodule.py



def greeting(name):
  print("Hello, " + name)



Use a Module



Now we can use the module we just created, by using the import statement:




Example


Import the module named mymodule, and call the greeting function:



import mymodule

mymodule.greeting("Jonathan")

Run example »


Note: When using a function from a module, use the syntax: module_name.function_name.





Variables in Module



The module can contain functions, as already described, but also variables of
all types (arrays, dictionaries, objects etc):




Example


Save this code in the file mymodule.py



person1 = {
  "name": "John",
  "age": 36,
 
"country": "Norway"
}




Example


Import the module named mymodule, and access the person1 dictionary:



import mymodule

a = mymodule.person1["age"]
print(a)

Run example »








<!--
mid_content, all: [300,250][336,280][728,90][970,250][970,90][320,50][468,60]
-->





Naming a Module



You can name the module file whatever you like, but it must have the file extension
.py



Re-naming a Module



You can create an alias when you import a module, by using the as keyword:




Example


Create an alias for mymodule called mx:



import mymodule as mx

a = mx.person1["age"]
print(a)

Run example »





Built-in Modules



There are several built-in modules in Python, which you can import whenever you like.




Example


Import and use the platform module:



import platform

x = platform.system()
print(x)

Run example »




Using the dir() Function



There is a built-in function to list all the function names (or variable
names) in a module. The dir() function:




Example


List all the defined names belonging to the platform module:



import platform

x = dir(platform)
print(x)

Run example »


Note: The dir() function can be used on all
modules, also the ones you create yourself.






Import From Module



You can choose to import only parts from a module, by using the from keyword.




Example


The module named mymodule has one function
and one dictionary:



def greeting(name):
  print("Hello, " + name)

person1
= {
  "name": "John",
  "age": 36,
  "country":
"Norway"
}




Example


Import only the person1 dictionary from the module:



from mymodule import person1

print (person1["age"])

Run example »


Note: When importing using the from
keyword, do not use the module name when referring to elements in the module.
Example: person1.age, not
mymodule.person1.age




<!--



Where to Store a Module?



You can store the modules in the same directory as the rest of your python files, or you can choose a different location.



Python will start by searching the current directory, then it will search in every location described in the PYTHONPATH variable.



To see which locations that are described in the PYTHONPATH variable, check the sys.path property:




Example


List all the locations described in the PYTHONPATH property:



import sys

print(sys.path)

Run example »


To add locations to the PYTHONPATH variable use the set PYTHONPATH statement:




Example


Add a new location to the PYTHONPATH property:



C:UsersYour Name>set PYTHONPATH = C:UsersYour Namepython_modules

Run example »


Note: The dir() function can be used on all
modules.



-->




❮ Previous
Next ❯

Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods