2

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?

john-hen
  • 4,410
  • 2
  • 23
  • 40
Hendrra
  • 682
  • 1
  • 8
  • 19
  • at the top of your program perhaps try: import sys sys.path.append(".") – Stan S. Jun 28 '19 at 22:26
  • @StanS. Unfortunately didn't work :( – Hendrra Jun 28 '19 at 22:28
  • 4
    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:](https://meta.stackoverflow.com/a/285557/2823755) – wwii Jun 29 '19 at 00:46

6 Answers6

10

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.

john-hen
  • 4,410
  • 2
  • 23
  • 40
3

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
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
1

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.

hygull
  • 8,464
  • 2
  • 43
  • 52
1

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
atronoush
  • 21
  • 2
0

Something similar happened to me. I could import from the terminal, running python. On spyder, with the same python version, I could not import the same package. After trying different things, reinstalling Spyder solved the issue.

0

Keep the python files in same folder and simply change the directory to the current python file location using import os os.chdir("module folder")

It will work. It just worked for me.:)