0

I am trying to figure out how to reference a function in a .py file from another .py file in the same directory. I have followed the python.org Modules instructions, but I can't get it to work.

Here is what I have so far:

This is the file `mymodule.py' that contains the function:

# -*- coding: utf-8 -*-

# Define plus_two function
def plus_two(x):
    print(x+2)

Here is the file that calls the function:

# -*- coding: utf-8 -*-

import mymodule
x=4
mymodule.plus_two(x)

It should print 6, but it just gives this error: ModuleNotFoundError: No module named 'mymodule'

The mymodule.py file has been saved, and I know the function works fine.

Eric Sims
  • 1
  • 1
  • 1
    Your code works fine for me. Are you sure you have those files in the same folder? Are these files part of a bigger project structure? – pacuna Aug 05 '20 at 23:19
  • @pacuna They are not part of a larger structure. It's literally just those two .py files. I am using Spyder, and I have them both saved on my desktop if that matters. – Eric Sims Aug 05 '20 at 23:22
  • 1
    [this](https://stackoverflow.com/questions/24722212/python-cant-find-module-in-the-same-folder) might help – Jannes Carpentier Aug 05 '20 at 23:22
  • Figured it out. Turns out I was pressing `Shift+Enter`, which works great in Jupyter notebooks but doesn't work in Spyder when not using `#%%` to break the code into cells... – Eric Sims Aug 05 '20 at 23:26

0 Answers0