• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Import Python File Into a Program in Python

Import Python File Into a Program in Python

Author: Aditya Raj
Last Updated: November 25, 2022

In python, we perform most of our tasks by importing different in-built libraries. In this article, we will discuss how we can In this article, we have discussed how to import a python file into a python program.

Import File From Current Working Directory

We can use any file as a module to import code. For this, we will use the import statement. For instance, suppose that we have the following file named sample_module.py in the current working directory.

The code in sample_module.py is as follows.

def print_module_name():
	print("Hi, I am in the sample module file.")

To import the functions defined sample_module.py, we can simply import the sample_module.py using the following statement.

import sample_module

Here, you can observe that we have only written the file name and not the file extension in the import statement. After importing the file, you can use the functions defined in the python file as shown below.

import sample_module

print("I am in example file. Calling functions from imported file.")
sample_module.print_module_name()

Output:

I am in example file. Calling functions from imported file.
Hi, I am in the sample module file.

Import File From a Different Directory Into a Python Program

Any python program can import the modules directly from certain directories. These directories are defined in the file path. To look at those directories, we can use the sys module. The path attribute in the sys module contains the name of all the directories from which the current python program can import other modules. You can print all such directories using sys.path attribute as shown below.

import sys
print(sys.path)

Output:

['/home/aditya1117/import python example', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/aditya1117/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.10/dist-packages']

If the file path of any directory is present in the sys.path attribute, we can import the module from that directory directly into our python file. You can observe that the current working directory "import python example" is present in the sys.path list. That’s why we were able to import the sample_module file directly into our program.

Hence, to import a file from another directory, we first have to add the file path of the directory to sys.path attribute. As we know that the sys.path attribute contains a list of file paths, we can append the file path of the directory to the python list using the append() method. After adding the file path of the directory to the sys.path attribute, we can import the python files defined in the directory into our current python program.  

You can observe this in the following example.

import sys
print("Hi, I am in example file.")
print(sys.path)
sys.path.append("/home/aditya1117/codes")
print("New file paths:")
print(sys.path)
import sample

sample.module_name_printer()

Output:

Hi, I am in example file.
['/home/aditya1117/import python example', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/aditya1117/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.10/dist-packages']
New file paths:
['/home/aditya1117/import python example', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/aditya1117/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.10/dist-packages', '/home/aditya1117/codes']
Hi, I am in sample.py file in \codes directory.

Conclusion

In this article, we have discussed how to import a python file into a python program. To know more about python programming, you can read this article on file handling in python. You might also like this article on list comprehension in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us