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?
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.
in my case i named my file
helper.scrap.py
and couldn't make it work until i changed tohelper.py
You can do this in 2 ways. First is just to import the specific function you want from file.py. To do this use
Another way is to import the entire file
Then you can call any function inside file.py using
First save the file in .py format (for example,
my_example.py
). And if that file have functions,In the calling function you just have to type the below lines.
file_name: my_example2.py
============================
============================
There isn't any need to add
file.py
while importing. Just writefrom file import function
, and then call the function usingfunction(a, b)
. The reason why this may not work, is becausefile
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 calledb.py
, you will need to make sure thata.py
andb.py
are in the same directory.Came across the same feature but I had to do the below to make it work.
If you are seeing 'ModuleNotFoundError: No module named', you probably need the dot(.) in front of the filename as below;