Cannot import my own module when running script in Spyder

11,150

Solution 1

In the Spyder IDE, as I can tell from your screenshot, the current working directory (displayed in the top right corner) is different from the directory in which your script resides (displayed on top of the Editor panel).

If you open the "Tools" menu, select "Preferences", and switch to the "Run" tab, you will find a box named "Working Directory settings" where you can choose between "the directory of the file being executed" or "the current working directory". I suspect, as it is, you have selected the latter. Which would explain why the module cannot be found.

With the default setting — "the directory of the file being executed" — Spyder would simply execute the script in its own folder, and the script would have no problem finding the module.

Solution 2

you are using spyder.

all you need to do is in spyder file explorer go to your project ie

/User/david/Document/Python/Project/

and then close the ipython terminal and start a new on (after you went to project folder) in the file explorer.

why your method not working, because ipython took the python execution path as current path opened in the file explorer (spyder)

run the code , it will work

other wise you need to provide relative path and use

 from . import WebScraper 
 x=WebScraper().function

Solution 3

By default, most of the apps have their default working directory.

Ex: If you will open command prompt, it's prompt is something like C:\Users\<username> in Windows, /Users/<username> in MAC.

So definitely, Spyder will also have its own working directory. Whenever you will execute any script, Spyder will try to look in that first.

My suggestion is to programatically check the working directory and navigate to the correct place (if we are in wrong place). Have a look at the below code.

import os
os.getcwd() # Check current directory's path
os.chdir('/Users/david/Documents/Python/Project') # Navigate

And after this try to import, it will work.

And if you wish, you can append this path to sys.path list.

Solution 4

For me on Spyder 5.1.5 and Python 3.7.9 I had to restart IPython kernel!

The symptom is like this.

  • I have two module m1 and m2 in the same directory
  • The module m1 import m2
  • Strat Spyder and run m1, everything works fine
  • Add module m3 and import from m1
  • Run m1, Spyder complains and raises the error ModuleNotFound
  • Restart Ipython Kernel, everything work fine and there is no error
Share:
11,150
Hendrra
Author by

Hendrra

I am undergraduate student particularly interested in the field of Harmonic Analysis. In my free time I also like reading books about theoretical Physics.

Updated on August 01, 2022

Comments

  • Hendrra
    Hendrra almost 2 years

    According to the official Python documentations or to this post importing own modules into scripts is quite easy. Basically I just need to create my .py files, save them in one directory and I can import them using just

    from my_module import my_function
    

    It is exactly what I did in my Project. I wrote two scripts and saved them inside one directory. I would like to use some functions from them in third script (again it is saved in the same directory). As in the picture below.

    My python scripts

    Now I import WebScraper.py in the following way enter image description here

    As you can see in the picture above there's an error which says that there is no module named WebScraper. How can I deal with that problem?

    • Stan S.
      Stan S. almost 5 years
      at the top of your program perhaps try: import sys sys.path.append(".")
    • Hendrra
      Hendrra almost 5 years
      @StanS. Unfortunately didn't work :(
    • wwii
      wwii almost 5 years
      Please don't post images of code/data/Tracebacks. Just copy the text, paste it in your question and format it as code. You should not post code as an image because: