0 votes
1 view
in Python by (33.2k points)

How do I import other files in Python?

  1. How exactly can I import a specific python file like import file.py?

  2. How can I import a folder instead of a specific file?

  3. I want to load a Python file dynamically at runtime, based on user input.

  4. I want to know how to load just one specific part of the file.

For example, in main.py I have:

from extra import *

Although this gives me all the definitions in extra.py, when maybe all I want is a single definition:

def gap():

Print

print

What do I add to the import statement to just get a gap from extra.py?

1 Answer

0 votes
by (107k points)
edited by

import importlib module

name = input('Enter module name:') 

importlib.import_module(moduleName)

To know more about this you can have a look at the following video tutorial:-

 

Related questions

0 votes
2 answers
asked Oct 3, 2019 in Python by Tech4ever (20.3k points)
0 votes
2 answers
0 votes
1 answer
asked Jul 10, 2019 in Python by Sammy (47.8k points)
+3 votes
2 answers
0 votes
1 answer
asked Oct 14, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...