1

It seems a simple problem so I thought it would already be answered, but none of the explanations I found on the internet contained a solution that was valid for me.

So, I just want to be able to import functions from a file that is in the same folder as the file where I want to use them. It was written everywhere that I just had to write import my_file, but I just get a ImportError: No module named my_file

I tried to add an empty __init__.py file in the folder but it didn't work. I didn't try other too complicated solutions because there should be a simple one and I feel like I'm just missing something basic yet indispensable.

So, I made a test as simple as possible with just a folder Folder containing two files, file1.py and file2.py. Here is the content of file1.py:

import file2
f()

And here is the content of file2.py:

def f():
    print "it works !"

When I execute file1.py, i get the following error at line 1: ImportError: No module named file2

(I tried with all possible combinations presence and absence of quotes, parentheses and .py extension, with most of them I get a SyntaxError: invalid syntax)

petezurich
  • 9,280
  • 9
  • 43
  • 57
Seldi
  • 57
  • 2
  • 9
  • Is`Folder` in the python path ? can you execute the following before importing : `import sys; print sys.path` – Thibault D. Jun 28 '19 at 09:11
  • 2
    Show exactly what you do to execute file1. – Daniel Roseman Jun 28 '19 at 09:11
  • 1
    https://stackoverflow.com/questions/2349991/how-to-import-other-python-files read this post please it will help u – İsa GİRİŞKEN Jun 28 '19 at 09:11
  • Do you execute the script in the folder? Or in other path – Wonka Jun 28 '19 at 09:13
  • This should "just work" (where the value of "work" is to produce the output `NameError: name 'f' is not defined`) if you execute `python file1.py`. – molbdnilo Jun 28 '19 at 09:16
  • Adding `import sys; print sys.path` at the beginning prints this: `['', 'C:\\Windows\\system32\\python27.zip', 'c:\\python27\\DLLs', 'c:\\python27\\lib', 'c:\\python27\\lib\\plat-win', 'c:\\python27\\lib\\lib-tk', 'c:\\python27', 'c:\\python27\\lib\\site-packages']`. I use the Pyzo IDE, so I just open my file with it and run it inside the IDE – Seldi Jun 28 '19 at 09:17
  • I also tried using the importlib module, but I think there's something I didn't get about the working directory – Seldi Jun 28 '19 at 09:19
  • It's most likely a problem with your IDE, and you need to study its documentation. – molbdnilo Jun 28 '19 at 09:19
  • I just tried adding the file to the system path (getting its path with `os.path.abspath(__file__)`) but I get `NameError: name '__file__' is not defined` – Seldi Jun 28 '19 at 09:32
  • Try `from .file2 import f` – heemayl Jun 28 '19 at 11:15
  • 1
    you should add `Folder` to your python path: `import sys; sys.path.append('')` – Thibault D. Jun 28 '19 at 12:51

4 Answers4

0

you should write like below in file1.py:

from file2 import f
f()
Guokas
  • 750
  • 7
  • 23
  • I get the same error `ImportError: No module named file2` – Seldi Jun 28 '19 at 09:29
  • Are you sure file1.py and file2.py in the same directory? – Guokas Jun 28 '19 at 09:32
  • it does not make sense if the file1.py and file2.py in the same directory. I suggest you Consider cleaning up the cache or reinstalling python – Guokas Jun 28 '19 at 09:39
  • Yes they are in the same directory. I tried with another IDE and it worked, after asking where to execute the code from, so I guess it was a problem of working directory. – Seldi Jun 28 '19 at 10:10
0

You are doing in a wrong way Below is the code by which you will get your desired result:-

file1

import file2
file2.f()   # You have to use like this.

file2

def f():
    print "it works !"

I hope it may help you.

Rahul charan
  • 765
  • 7
  • 15
  • I still get the same error `ImportError: No module named file2`. file2 cannot be reached for import, so the way I call the function is not the problem – Seldi Jun 28 '19 at 10:04
  • @Seldi it is working properly in my laptop. If you have used the code given above then it should work. – Rahul charan Jun 28 '19 at 10:08
  • @Seldi Also make sure that both file1 and file2 are with `.py` extension – Rahul charan Jun 28 '19 at 10:10
  • I did it, but it seems that the problem was coming from the IDE – Seldi Jun 28 '19 at 10:15
  • @Seldi Make 2 files with these name with the help of notepad with `.py` extension like `file1.py` AND `file2.py`, AND then simplly run them into your powershell by `python file1.py` and if it is working then you will get your result. If it is working here then it can work in any IDE. – Rahul charan Jun 28 '19 at 10:18
0

Please use this, working fine for me.

file1

from  file2 import f

#You will get object code. it means working fine.

print (f)
Community
  • 1
  • 1
Yash Shukla
  • 141
  • 6
0

I tried with another IDE (Spyder instead of Pyzo). It asked where to execute the code from, I clicked "from the file's location" and it worked.

I guess the problem was about where the code was executed from...

Seldi
  • 57
  • 2
  • 9