Back

Explore Courses Blog Tutorials Interview Questions
0 votes
5 views
in Python by (33.1k 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 (106k 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:-

If you are looking for upskilling yourself in python you can join our Python Training and learn from the industry expert. 

Related questions

0 votes
1 answer
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.6k points)
+3 votes
2 answers

Browse Categories

...