5

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?

3
  • at the top of your program perhaps try: import sys sys.path.append(".") Commented Jun 28, 2019 at 22:26
  • @StanS. Unfortunately didn't work :( Commented Jun 28, 2019 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: Commented Jun 29, 2019 at 0:46

6 Answers 6

11

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.

Sign up to request clarification or add additional context in comments.

Comments

4

You are using Spyder.

All you need to do is in Spyder's file explorer go to your project, i.e.

/User/david/Document/Python/Project/

Then close the IPython terminal and start a new one (after you went to project folder) in the file explorer.

Your method was not working because IPython took the python execution path as the current path opened in the file explorer (Spyder).

Run the code; it will work.

Otherwise, you need to provide a relative path and use:

 from . import WebScraper 
 x=WebScraper().function

Comments

2

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

Comments

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.

Comments

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.

Comments

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.:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.