Search code examples
pythonpython-importspyder

Spyder can't import local module while command line runs fine


I'm writing scripts with Spyder (Anaconda 2021.05 which has python 3.8.8, IPython 7.22.0, spyder 4.2.5, Windows) but the script needs to be run under Anaconda command line, so I want to find a way of import that works in both setup.

My folders look like this

project/processes/main.py
project/utils/date.py

and in main.py, I add my module into sys.path

import os, sys
sys.path.append(os.path.dirname(os.getcwd()))
print(sys.path)

import utils.date

This runs fine under command line, but under spyder it says

*** ModuleNotFoundError: No module named 'utils.date'; 'utils' is not a package

The print of sys.path is different under command line and under spyder (as expected), but they both include the project folder. How could I make spyder/ipython see my local module?


Solution

  • The issue comes from having 2 spyder instances running. When I close both and start only 1, it imports fine.