question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ModuleNotFoundError: Import a Module from Same Directory Does Not Work

See original GitHub issue

Cannot import my own module when running script in Spyder

Problem Description

Importing a module from the same working directory raises error

ModuleNotFoundError: No module named ...

What steps reproduce the problem?

  1. in a test folder there two python file test.py and mod01.py. test imports mod01
  2. start Spyder, open test, run test, everything works no error
  3. add a new module in the same directory mod02.py and import from test
  4. run test, you see the error ModuleNotFoundError: No module named 'mod02'
  5. close spyder, reopen it
  6. run test, everything works fine!!!

Another workaround instead of step 5, just Restart IPython Kernel, again running test produces no error

See discussion here: stackoverflow

What is the expected output? What do you see instead?

I expect adding module in a Spyder session to the same working directory can be imported in my test file! I do not expect to have to restart Spyder or Ipython kernel! If I work with IDLE, I have not such problem.

The content of test and module files

  • test.py
import mod01
import mod02

print('works?')
  • mod01.py
a=1
  • mod02.py
b=1

Versions

  • Spyder version: 5.1.2
  • Python version: 3.7.9
  • Qt version:
  • PyQt version:
  • Operating System name/version: Windows 10

Dependencies


# Mandatory:
atomicwrites >=1.2.0          :  1.4.0 (OK)
chardet >=2.0.0               :  4.0.0 (OK)
cloudpickle >=0.5.0           :  1.6.0 (OK)
cookiecutter >=1.6.0          :  1.7.2 (OK)
diff_match_patch >=20181111   :  20200713 (OK)
intervaltree >=3.0.2          :  3.1.0 (OK)
IPython >=7.6.0               :  7.27.0 (OK)
jedi >=0.17.2;<0.19.0         :  0.17.2 (OK)
jsonschema >=3.2.0            :  3.2.0 (OK)
keyring >=17.0.0              :  21.8.0 (OK)
nbconvert >=4.0               :  6.0.7 (OK)
numpydoc >=0.6.0              :  1.1.0 (OK)
paramiko >=2.4.0              :  2.7.2 (OK)
parso >=0.7.0;<0.9.0          :  0.7.0 (OK)
pexpect >=4.4.0               :  4.8.0 (OK)
pickleshare >=0.4             :  0.7.5 (OK)
psutil >=5.3                  :  5.8.0 (OK)
pygments >=2.0                :  2.7.4 (OK)
pylint >=2.5.0;<2.10.0        :  2.6.0 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.2.2;<1.3.0          :  1.2.2 (OK)
pylsp_black >=1.0.0           :  None (OK)
qdarkstyle =3.0.2             :  3.0.2 (OK)
qstylizer >=0.1.10            :  0.1.10 (OK)
qtawesome >=1.0.2             :  1.0.2 (OK)
qtconsole >=5.1.0             :  5.1.0 (OK)
qtpy >=1.5.0                  :  1.9.0 (OK)
rtree >=0.9.7                 :  0.9.7 (OK)
setuptools >=49.6.0           :  57.4.0 (OK)
sphinx >=0.6.6                :  3.4.3 (OK)
spyder_kernels >=2.1.1;<2.2.0 :  2.1.1 (OK)
textdistance >=4.2.0          :  4.2.0 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog >=0.10.3             :  1.0.2 (OK)
zmq >=17                      :  21.0.1 (OK)

# Optional:
cython >=0.21                 :  None (OK)
matplotlib >=2.0.0            :  3.4.3 (OK)
numpy >=1.7                   :  1.21.2 (OK)
pandas >=1.1.1                :  1.3.1 (OK)
scipy >=0.17.0                :  1.7.1 (OK)
sympy >=0.7.3                 :  1.8 (OK)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kookmacommented, Dec 2, 2021

@dalthviz - sorry for late reply. I totally missed this! Yes in Spyder 5.1.5 the problem has been solved! Much appreciated!

1reaction
Poikiloscommented, May 29, 2022

If you found this issue like I did but the reason for the error is that you can’t import a module from a different site-packages directory, you must change your Python interpreter in preferences to the one used to install the package (Editing PYTHONPATH isn’t the solution in this case–See Issue adding site-packages directory to PYTHONPATH in Spyder on StackOverflow).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python can't find module in the same folder - Stack Overflow
2. How are you executing the script? · 1. Try >>> import test · @Casy_fill Do you run your program from the directory,...
Read more >
ModuleNotFoundError: No module named x
How does module import work behind the scenes? Now let's assume that in your current module, you wish to import another module as...
Read more >
ModuleNotFoundError: no module named Python Error [Fixed]
How to fix the ModuleNotFoundError in Python · 1. Make sure imported modules are installed · 2. Make sure modules are spelled correctly...
Read more >
Python – Import module from different directory - GeeksforGeeks
Now don't worry if your module is not imported correctly. In this article, we will discuss ways to import a module from another...
Read more >
Traps for the Unwary in Python's Import System
With that layout and the current working directory being project , Python 2.7 ... While Python 3.3+ is able to import the submodule...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found