Home > Net >  What is modules in python? How to import module in python?
What is modules in python? How to import module in python?

Time:12-09

I want to ask that how to import modules in python like if we want to import freegames module in python than how to import it in python.

I want to try to know about the modules in python and how to install it in python.

CodePudding user response:

If you wanted to use the requests module you would want to first pip install requests inside of a local terminal, to use the module then type import requests inside your python file.

I would recommend watching this video to learn more https://www.youtube.com/watch?v=1RuMJ53CKds

CodePudding user response:

in python , a module is a self contained file that contains python code , such as functions , classes and variables . modules allow us to reuse and organize our code in a logical and modular way and share your code .

to import a module in python , you can use the import keyword followed by the name of the module . for example to import math module , which contains mathematical functions and constants , you can use following codes :

import math

we can also import specific function or variables from a module using from keyword

example : from math import sin

overall importing modules in python allows us to use and reuse code in a modular way

CodePudding user response:

Module is a file-like object that contains names defined in that file. Modules are just python files, mostly containing code that can be used in one development topic. For example, pygame is a module for creating games on python, math is a module that includes math operations.

There are standart library modules and PyPi modules. For standart library modules you just need to them like this:

import some_module_name

without .py postfix.

More about importing modules you can learn from google, there is also relarive import, importlib, __import__ function, many interesting things.

To use PyPi module you need to install it. Google name of module you wanna use if you dont know it already. Open cmd.exe and type:

pip install your_module

You will see some progressbars and some scary labels. If it didn't work, you need to add python to environment variable PATH. You can also google this problem, it has simple solution.

If you are using PyCharm, you need to install module in your project with pycharm tools.

When you installed the module, it will act like any other module in library.

To use something from module, you need to import it and type help(your_module) to see documentation if there is documentation. Using it you can call things from module like this:

your_module.variable
your_module.some_function
your_module.some_class
your.module.submodule

If module includes other modules, its a package.

I have no idea why are you asking about that wide topic in one question. I gave you basic info, you need more info to normally use all this stuff. So, the favourite word in programming - google.

  • Related