0

I have 2 files: print.py and cool.py. They are in the same map.

print.py:

def test():
    print('cool')

cool.py:

from print import test

test()

I get error: ImportError: cannot import name 'test' from 'print' (c:\Users\me\OneDrive\Creative Cloud Files\Dokument\Projects\project1\print.py)

Why am I getting an error here? If I write import & they are in the same map, shouldn't I be able to use the functions in a different file?

Jocko
  • 39
  • 1
  • 6
  • 1
    Since `print` is a reserved, built-in word, I'd think that'd break things. You should try using a unique name instead. – Random Davis Dec 28 '20 at 22:11
  • You should try and rename print.py to something which does not use a name already used in python. – quamrana Dec 28 '20 at 22:11
  • The name `print` shouldn't be an issue here since it isn't reserved (although it's certainly not good practice to potentially shadow the built-in with a module name). Is the code that you have here exactly what you have in your real code, and are you sure that they're in the same directory? – Carcigenicate Dec 28 '20 at 22:16
  • I changed the names & it still doesn't work – Jocko Dec 28 '20 at 22:22
  • 1
    Ya, the name won't have any bearing here. I tested it an `print` works fine as a module name (at least in Python 3). Try changing the import to just `import print`, then do `print(dir(print))`. What do you get? – Carcigenicate Dec 28 '20 at 22:26
  • ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__'] – Jocko Dec 28 '20 at 22:33
  • So, you appear to be looking at the wrong file, or `print.py` never saved correctly. Make sure that your `print.py` is correctly saved into the folder that you expect (double check with an external editor), and restart your IDE if necessary. – Carcigenicate Dec 28 '20 at 22:45
  • Fixed it by running the imported file beforehand. After creating print.py I never ran it since I didn't doubt what it did. Apparently vscode doesn't really recognize the file unless I run it once before. – Jocko Dec 28 '20 at 23:29

0 Answers0