Python function different file

How to import python functions from different file (in Ubuntu)

I have a python file called hero.py that refers to other python files located in views.py (Both these files exist in the same folder). hero.py code :

 #!/usr/bin/env python3 from .views import main, returnSum, most_frequent, find_mine_site_view_id, get_user_Activity, initialise_analytics_reporting list_of_mines = ['mine1', 'mine2', 'mine3'] start_date = 'yesterday' end_date = 'yesterday' main(list_of_mines, start_date, end_date) 

After making the file executable with chmod +x hero.py and adding #!/usr/bin/env python3 at the top of hero.py , I get this error when running ./hero.py :

Traceback (most recent call last): File "./hero.py", line 2, in from .views import main, returnSum, most_frequent, find_mine_site_view_id, get_user_Activity, initialise_analytics_reporting ModuleNotFoundError: No module named '__main__.views'; '__main__' is not a package 

I am aware that my views.py is not a package, I simply want to import the functions that exist within views.py Not sure if it is an Ubuntu thing. Please help When running ls -la in the folder where both files exist:

total 72 drwxrwxr-x 8 llewellyn llewellyn 4096 May 13 06:39 . drwxrwxr-x 6 llewellyn llewellyn 4096 May 11 19:19 .. drwxrwxr-x 3 llewellyn llewellyn 4096 May 7 08:52 .idea -rw-rw-r-- 1 llewellyn llewellyn 0 May 7 07:21 __init__.py drwxrwxr-x 2 llewellyn llewellyn 4096 May 13 06:18 __pycache__ -rwxrwxr-x 1 llewellyn llewellyn 86 May 12 17:39 admin.py -rwxrwxr-x 1 llewellyn llewellyn 108 May 12 17:40 apps.py drwxrwxr-x 3 llewellyn llewellyn 4096 May 7 09:04 config drwxrwxr-x 3 llewellyn llewellyn 4096 May 9 11:34 migrations -rwxrwxr-x 1 llewellyn llewellyn 2607 May 12 17:40 models.py -rwxrwxr-x 1 llewellyn llewellyn 16146 May 13 06:17 views.py 

Источник

Читайте также:  Готовый скрипты на питоне

SOLVED: Calling a function from another file in Python

Different methods for calling a function from another file in Python

When we wish to call a function from another file in Python, we have different scenarios to achieve our goal.

  • Calling a function from another file
  • Calling a function containing arguments from another python file
  • Calling a function present in a file with a different directory
  • Importing all functions from another Python file
  • Calling a function without using the import function

Example-1: Calling a function from another file

In this scenario, we are calling a function from another file. Let us take a compute.py file having a function interest to compute the simple interest of given principal and duration. We will then write a demo.py file that has saving function, which when called makes call to interest function to compute simple interest.
compute.py

# Function to compute simple interest at fixed rate of 5% def interest(): p=int(input("Enter the principal amount")) n=int(input("Enter the number of years")) print("Computing simple interest at rate of 5%") r=5 si=(p*r*n)/100 return si 
from compute import interest def saving(): print("Interest accrued is",interest()) saving() 
Enter the principal amount 1000 Enter the number of years 2 Computing simple interest at rate of 5% Interest accrued is 100.0 

Example-2: Calling Function containing arguments from another python file

In this scenario, we are calling a function from another file but with the arguments. Let us firstly write two python files compute.py and demo.py. We will write a function interest to compute simple interest when we pass amount and number of years. The rate is fixed as 5%. Hence, the function will compute simple interest and return the amount to the calling function.
compute.py

# Function to compute simple interest at fixed rate of 5% def interest(p,n): print("Computing simple interest at rate of 5%") r=5 si=(p*r*n)/100 return si 
from compute import interest # Get input from the user p=int(input("Enter the principal amount")) n=int(input("Enter the number of years")) # Calling a function of compute.py file with p and n as an arguments print("Interest accrued for the principal",p, "for",n, "years at the rate of 5% is",interest(p,n)) 
Enter the principal amount 1000 Enter the number of years 2 Computing simple interest at rate of 5% Interest accrued for the principal 1000 for 2 years at the rate of 5% is 100.0 

Example-3: Calling function present in a file with a different directory

In this scenario, we are calling a function from another file in different directory. Let us save the file compute.py inside the folder bank. Whereas, demo.py is saved outside the folder bank. Hence, we are accessing the file from the module bank stored in the different directory. In this case, only the import statement requires the modification. The way function is called remains same.

Читайте также:  Apt update apt upgrade pkg install python pip install upgrade pip

bank/compute.py

# Function to compute simple interest at fixed rate of 5% def interest(p,n): print("Computing simple interest at rate of 5%") r=5 si=(p*r*n)/100 return si 
from bank.compute import interest # Get input from the user p=int(input("Enter the principal amount")) n=int(input("Enter the number of years")) # Calling a function of compute.py file with p and n as an arguments print("Interest accrued for the principal",p, "for",n, "years at the rate of 5% is",interest(p,n)) 
Enter the principal amount 1000 Enter the number of years 2 Computing simple interest at rate of 5% Interest accrued for the principal 1000 for 2 years at the rate of 5% is 100.0 

Example-4: Importing all functions from another Python file

In this scenario, Let us take the compute.py file but with one more function added to compute compound interest. Here, we will import all the functions present in compute.py file. The compute.py contains two functions to compute simple interest and compound interest. We will then make a call to a function demo.py file.

# Function to compute simple interest at fixed rate of 5% def interest(p,n): print("Computing simple interest at rate of 5%") r=5 si=(p*r*n)/100 return si # Function to compute Compound interest at fixed rate of 5% def compoundinterest(p,n): print("Computing Compound interest at rate of 5%") r=5 ci=p*pow(1+r/100,n)-p return ci 
from compute import * # Get input from the user p=int(input("Enter the principal amount")) n=int(input("Enter the number of years")) # Calling a function of compute.py file with p and n as an arguments print("Simple Interest accrued for the principal",p, "for",n, "years at the rate of 5% is",interest(p,n)) print("Compound Interest accrued for the principal",p, "for",n, "years at the rate of 5% is",compoundinterest(p,n)) 
Enter the principal amount 1000 Enter the number of years 5 Computing simple interest at rate of 5% Simple Interest accrued for the principal 1000 for 5 years at the rate of 5% is 250.0 Computing Compound interest at rate of 5% Compound Interest accrued for the principal 1000 for 5 years at the rate of 5% is 276.2815625000003 

Example-5: Call a function without using the import statement

In this scenario, we will not use import statement, rather use importlib and use the function from the file compute.py . Let us assume, our project structure is something like as shown below.
computeproject->computelib->compute.py
computeproject->demo.py

# Function to compute simple interest at fixed rate of 5% def interest(p,n): print("Computing simple interest at rate of 5%") r=5 si=(p*r*n)/100 print("Simple Interest accured for the principal",p, "for",n, "years at the rate of 5% is",si) 
import importlib from inspect import isfunction def call_func(full_module_name, func_name, *argv): module = importlib.import_module(full_module_name) for attribute_name in dir(module): attribute = getattr(module, attribute_name) if isfunction(attribute) and attribute_name == func_name: attribute(*argv) call_func('compute', 'interest', 1000,5) 
Computing simple interest at rate of 5% Interest accured for the principal 1000 for 5 years at the rate of 5% is 250.0

Summary

The knowledge of Calling a function from another file in Python is very useful while working on real and practical world applications. It helps in re-usability and data abstraction. In many situations, we will need to call a function that is used in many other files of a project and is lying in some common functionality file. In this tutorial, we covered the different scenario to call a function lying in another file and directory with an example. All in all, this tutorial, covers everything that you need to know in order to understand calling a function from another file in Python.

Источник

Оцените статью