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.

How to configure Spyder preferences to behave like other terminals with respect to absolute and relative imports?

See original GitHub issue

Issue 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 (or pip, 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
DOH-Manadacommented, Nov 10, 2021

You might need a blank init.py to do relative imports.

0reactions
hlmhlrcommented, Jan 19, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spyder not handling relative imports - Stack Overflow
I am attempting to python3 proof my work and spyder seems to be having an issue with absolute_import .
Read more >
Simple trick to work with relative paths in Python | by Mike Huls
The most straight-forward method is just using an absolute path in our code. import json f = open(r'C:\projects\relative_path\data\mydata.json')
Read more >
Fix Python Relative Imports and Auto-completion in VSCode
... learn how to properly handle Python relative imports without extending the sys.path. Additionally, you will learn how to enable auto-...
Read more >
Working with Spyder — Spyder 5 documentation
Working with Spyder ¶. In this section, you will learn about Spyder's more advanced functionality, and explore most of the panes. Beyond the...
Read more >
Financial Data Analysis with Spyder
You can set up your environment in two different ways. Important. We recommend creating the virtual environment with Anaconda (or Miniconda) as it...
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