Call a function from another file in Python

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

Set_up: I have a .py file for each function I need to use in a program.

In this program, I need to call the function from the external files.

I've tried:

from file.py import function(a,b) 

But I get the error:

ImportError: No module named 'file.py'; file is not a package

How do I fix this problem?

回答1:

There isn't any need to add file.py while importing. Just write from file import function, and then call the function using function(a, b). The reason why this may not work, is because file is one of Python's core modules, so I suggest you change the name of your file.

Note that if you're trying to import functions from a.py to a file called b.py, you will need to make sure that a.py and b.py are in the same directory.



回答2:

First of all you do not need a .py.

If you have a file a.py and inside you have some functions:

def b():   # Something   return 1  def c():   # Something   return 2 

And you want to import them in z.py you have to write

from a import b, c 


回答3:

You can do this in 2 ways. First is just to import the specific function you want from file.py. To do this use

from file import function 

Another way is to import the entire file

import file as fl 

Then you can call any function inside file.py using

fl.function(a,b) 


回答4:

First save the file in .py format (for example, my_example.py). And if that file have functions,

def xyz():          --------          --------  def abc():          --------          -------- 

In the calling function you just have to type the below lines.

file_name: my_example2.py

============================

import my_example.py   a = my_example.xyz()  b = my_example.abc() 

============================



回答5:

You don't have to add file.py.

Just keep the file in the same location with the file from where you want to import it. Then just import your functions:

from file import a, b 


回答6:

You should have the file at the same location as that of the Python files you are trying to import. Also 'from file import function' is enough.



回答7:

Suppose the file you want to call is anotherfile.py and the method you want to call is method1, then first import the file and then the method

from anotherfile import method1 

if method1 is part of a class, let the class be class1, then

from anotherfile import class1 

then create an object of class1, suppose the object name is ob1, then

ob1 = class1() ob1.method1() 


回答8:

You can call the function from a different directory as well, in case you cannot or do not want to have the function in the same directory you are working. You can do this in two ways (perhaps there are more alternatives, but these are the ones that have worked for me).

Alternative 1 Temporarily change your working directory

import os  os.chdir("**Put here the directory where you have the file with your function**")  from file import function  os.chdir("**Put here the directory where you were working**") 

Alternative 2 Add the directory where you have your function to sys.path

import sys  sys.path.append("**Put here the directory where you have the file with your function**")  from file import function 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!