Skip to content
← Back to Community
How to modules? In python
Profile icon
YvngBashit

So I have been learning python and what interests me the most are modules. But the problem is I don't know how to use them?

Voters
Profile icon
sapnaattarde
Profile icon
YvngBashit
Comments
hotnewtop
Profile icon
JoshDaBosh

You would need to import them. For example, to use the time module, you'd do:

import time

and you could use it's functions like time.time(), time.sleep(), etc etc

Profile icon
ash15khng

Modules are basically allowing code from other files being used in another file. For example: import time imports the time module, allowing all of the functions in time to be used. To use a function from a module when you import a module as import modulename, use modulename.functionname(arg1, arg2="abcdefg")

Profile icon
DeadWither

Modules are files which contain special functions that Python does not compile. When importing a module, you enable the editor to compile the functions in the module.

I am going to import the module random

import random

But this only imports common functions. To import them all, you do this:

from random import *

The * means everything.
Now when importing a module, you import lots of functions which takes more storage data. If you want to use a specific one, you can only import that. For example I want to import a function (randint, which chooses a random number), I do this:

from random import randint

You can use it to do fun stuff

from random import randint print("A random number from 1 to 10 is:" + random.randint(1,10))
Profile icon
michael2008

On a Python or JavaScript repl, you can search for a package to install by clicking on the icon on the sidebar in the workspace. Simply search for the package you want and select it to install the package or to view its documentation. Clicking on the Add Package icon will put it in a spec file and a lock file.