How to import other Python files

0 votes

How do I import other files in Python?

  1. How exactly can I import a specific python file like import
  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 from the file.

For example, in I have:

from extra import * 

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

def gap():

What do I add to the import statement to just get gap from

Nov 26, 2020 in Python by Rajiv
• 8,880 points

1 answer to this question.

0 votes
  1. Just import file without the '.py' extension.

  2. You can mark a folder as a package, by adding an empty file named

  3. You can use the __import__ function. It takes the module name as a string. (Again: module name without the '.py' extension.)

    pmName = input('Enter module name:')
    pm = __import__(pmName)

    Type help(__import__) for more details.

answered Nov 26, 2020 by Gitika
• 65,870 points

Related Questions In Python

0 votes
1 answer

How to import other Python files?

importlib is recent addition in Python to ...READ MORE

answered Jul 27, 2018 in Python by Priyaj
• 58,100 points
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,720 points
0 votes
0 answers

How to mix read() and write() on Python files in Windows

It appears that a write() immediately following a read() on a ...READ MORE

Oct 23, 2018 in Python by Aryya
• 500 points
0 votes
0 answers

How to read lines from compressed text files in Python

Is it easy to read a line ...READ MORE

Dec 27, 2018 in Python by Anirudh
• 2,080 points
+1 vote
2 answers

Python with Selenium issue: “Chrome is being controlled by automated test software”

from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option("useAutomationExtension", False) chrome_options.add_experimental_option("excludeSwitches",["enable-automation"]) driver ...READ MORE

answered Apr 20, 2020 in Selenium by Manoj
–1 vote
1 answer

Selenium XPath error:- Element not found

The reason for Element not found exception ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,520 points
+2 votes
2 answers

How can I press ENTER key with the execute_script in selenium python?

The below code containing Keys.ENTER might just ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,520 points
0 votes
2 answers
+1 vote
3 answers

How can I use python to execute a curl command?

For sake of simplicity, maybe you should ...READ MORE

answered Oct 11, 2018 in Python by charlie_brown
• 7,780 points