How to configure Spyder preferences to behave like other terminals with respect to absolute and relative imports?
See original GitHub issueIssue Report Checklist
- Searched the issues page for similar reports
- Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
- Reproduced the issue after updating with
conda update spyder
(orpip
, if not using Anaconda) - Could not reproduce inside
jupyter qtconsole
(if console-related) - Tried basic troubleshooting (if a bug/error)
- Restarted Spyder
- Reset preferences with
spyder --reset
- Reinstalled the latest version of Anaconda
- Tried the other applicable steps from the Troubleshooting Guide
- Completed the Problem Description, Steps to Reproduce and Version sections below
Problem Description
Closed issue #4258 got side-tracked with respect to configuring Spyder to run absolute imports.
If I want to import .py
scripts, I need to use relative imports from my source folder in Spyder, but I do not have to when running from WSL.
If I change to relative imports, WSL treats my source folder as a module.
How can I/Is it possible to configure Spyder so I get the same absolute import behaviour as when I run from WSL?
What steps reproduce the problem?
script_name.py
and get_data.py
are located in ~/folder_name/src/
.
If script_name.py
is formatted like this and get_data.py
is located in a src folder.
import get_data
#do stuff with get_data
This treats get_data.py
as an absolute import in WSL and will run properly with the following command (from folder_name
),
python3 src/script_name.py
If I try to run this in Spyder with the following command,
runfile('folder_name/src/script_name.py', wdir='folder_name')
Traceback (most recent call last):
File "folder_name\src\script_name.py", line 1, in <module>
import src.get_data as get_data #get_data
ModuleNotFoundError: No module named 'src'
The proper way to handle this in Spyder is with a relative import
import src.get_data as get_data
#do stuff with get_data
But this produces the same error if I’m trying to run script_name.py
from WSL.
Traceback (most recent call last):
File "src/script_name.py", line 1, in <module>
import src.get_data as get_data #get_data
ModuleNotFoundError: No module named 'src'
What is the expected output? What do you see instead?
Is it possible to configure Spyder’s preferences or the run command to match how WSL or other terminals run python such that I do not have to use relative imports, i.e., avoid having to use src.get_data
?
Paste Traceback/Error Below (if applicable)
Traceback (most recent call last):
File "src/script_name.py", line 1, in <module>
import src.get_data as get_data #get_data
ModuleNotFoundError: No module named 'src'
Versions
- Spyder version: spyder 4.1.5 py37_0
- Python version: python 3.7.9 h60c2a47_0
- Qt version: qt 5.9.7 vc14h73c81de_0
- PyQt version: pyqt 5.9.2 py37h6538335_2
- Operating System name/version: WSL 2 on Windows 10 with Python 3.6.9
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
You might need a blank init.py to do relative imports.
Hi,
I am sorry but i am writing here with closed status of the issue but the error i am getting is closely related to this one, so i may get solution from this instantly.
The code, i am trying to run uses relative paths, so I created new project in Spyder, selected project path and trying to run the following command from spyder ipython console:
runfile(‘keras-retinanet/keras_retinanet/bin/train.py’, args=‘–weights resnet50_coco_best_v2.1.0.h5 --batch-size 2 --steps 5 --epochs 5 --snapshot-path TrainingOutput/snapshots --tensorboard-dir TrainingOutput csv dataset/train.csv dataset/classes.csv’, wdir=‘/home/hlr/Documents/new_test’)
This throws the following error: Kindly help me out in resolving this matter. Thanks,
from … import layers
ImportError: attempted relative import with no known parent package