Spyder: IPython consoles are not independent in terms of loading modules on Windows

Created on 7 Aug 2019  路  10Comments  路  Source: spyder-ide/spyder

Issue Report Checklist

  • [X] Searched the issues page for similar reports
  • [X] Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
  • [X] Reproduced the issue after updating with conda update spyder (or pip, if not using Anaconda)
  • [X] Could not reproduce inside jupyter qtconsole (if console-related)
  • [X] Tried basic troubleshooting (if a bug/error)

    • [X] Restarted Spyder

    • [X] Reset preferences with spyder --reset

    • [X] Reinstalled the latest version of Anaconda

    • [X] Tried the other applicable steps from the Troubleshooting Guide

  • [X] Completed the Problem Description, Steps to Reproduce and Version sections below

Problem Description

If you open a second ipython console and run a script that imports a local file that shares its filename with a module imported by the first ipython console, the import fails.

What steps reproduce the problem?

  1. Create the following folder structure (with the content shown below):

    • one/one.py

    • one/utilities.py

    • two/two.py

    • two/utilities.py

  2. Open one/one.py and two/two.py in the editor.
  3. With one/one.py open, click the "Run" button.

    • Output (which is correct) looks like this:

      In [1]: runfile('C:/spydertest/one/one.py', wdir='C:/spydertest/one')
      Utility 1
      
      In [2]: 
      
  4. Click the gear icon in the top-right of the console window and click "Open an IPython console".
  5. Switch to the two/two.py tab in the editor.
  6. Click the "Run" button.
  7. Exception is raised. If you do this in the opposite order (running two.py, opening a new console, running one.py), the exception is raised when running one.py instead.

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

Expected output:

In [1]: runfile('C:/spydertest/two/two.py', wdir='C:/spydertest/two')
Utility 2

In [2]:

Actual output:

In [1]: runfile('C:/spydertest/two/two.py', wdir='C:/spydertest/two')
Traceback (most recent call last):

  File "<ipython-input-1-9cf34275bbc9>", line 1, in <module>
    runfile('C:/spydertest/two/two.py', wdir='C:/spydertest/two')

  File "c:\applications\development\languages\python\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "c:\applications\development\languages\python\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/spydertest/two/two.py", line 1, in <module>
    from utilities import two_utility

ImportError: cannot import name 'two_utility' from 'utilities' (C:\spydertest\one\utilities.py)


In [2]: 

Files used to demonstrate issue:

one/one.py:

from utilities import one_utility

print(one_utility())

one/utilities.py:

def one_utility():
    return "Utility 1"

two/two.py:

from utilities import two_utility

print(two_utility())

two/utilities.py:

def two_utility():
    return "Utility 2"

Paste Traceback/Error Below (if applicable)

See above.

Versions

  • Spyder version: 3.3.6
  • Python version: 3.7.1 64-bit
  • Qt version: 5.6.2
  • PyQt version: 5.6
  • Operating System name/version: Windows 10

Dependencies

IPython >=4.0     :  7.7.0 (OK)
cython >=0.21     :  0.29.12 (OK)
jedi >=0.9.0      :  0.13.3 (OK)
matplotlib >=2.0.0:  2.2.2 (OK)
nbconvert >=4.0   :  5.5.0 (OK)
numpy >=1.7       :  1.16.4 (OK)
pandas >=0.13.1   :  0.25.0 (OK)
pycodestyle >=2.3 :  2.5.0 (OK)
pyflakes >=0.6.0  :  2.1.1 (OK)
pygments >=2.0    :  2.4.2 (OK)
pylint >=0.25     :  2.3.1 (OK)
qtconsole >=4.2.0 :  4.5.2 (OK)
rope >=0.9.4      :  0.14.0 (OK)
sphinx >=0.6.6    :  2.1.2 (OK)
sympy >=0.7.3     :  1.4 (OK)
Windows IPython Console Bug

All 10 comments

Please add empty __init__.py files to both your one and two directories to make them proper Python packages and try again.

I've added the empty __init__.py files but it made no difference to the outcome.

I wouldn't expect to have to have __init__.py files as this isn't a python module, this is an application (with one.py as the entry point). I wouldn't usually have an __init__.py in this situation even in python 2.7, but in python 3 the files aren't required anyway: quoting PEP 420:

Allowing implicit namespace packages means that the requirement to provide an __init__.py file can be dropped completely,

I can't reproduce this using latest master on linux.

@abudden Are you able to see if this is still a problem on syder4 beta4?

Thanks for testing this @bcolsen!

@dalthviz, are you able to reproduce this on Windows?

@ccordoba12 I was able to reproduce this on Windows using Spyder 3.3.6

Main while @abudden a work around you could try is to not use the same name for the utilities file. I tested renaming them to one_utilities.py and two_utilities.py and worked for me.

@dalthviz, please try with the latest master of Spyder and spyder-kernels. PR spyder-ide/spyder-kernels#131 could have solved this problem.

@ccordoba12 testing with master of Spyder and Spyder-kernels I get the same error:

runfile('C:/Users/Daniel/Documents/Spyder/foo_folder/one/one.py', wdir='C:/Users/Daniel/Documents/Spyder/foo_folder/one')
Traceback (most recent call last):

  File "<ipython-input-1-a6f57d3c5a2f>", line 1, in <module>
    runfile('C:/Users/Daniel/Documents/Spyder/foo_folder/one/one.py', wdir='C:/Users/Daniel/Documents/Spyder/foo_folder/one')

  File "c:\users\daniel\documents\spyder\spyder-kernels\spyder_kernels\customize\spydercustomize.py", line 890, in runfile
    execfile(filename, namespace)

  File "c:\users\daniel\documents\spyder\spyder-kernels\spyder_kernels\customize\spydercustomize.py", line 116, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Daniel/Documents/Spyder/foo_folder/one/one.py", line 1, in <module>
    from utilities import one_utility

ImportError: cannot import name 'one_utility' from 'utilities' (C:\Users\Daniel\Documents\Spyder\foo_folder\two\utilities.py)

@dalthviz, are you able to reproduce this in IPython terminal or in qtconsole?

@ccordoba12 I was unable to reproduce this in QtConsole executing the files with %run. Also, when using %run inside Spyder it works as expected

@bcolsen Apologies for going quiet for a few days, I'm afraid I haven't been able to test on the latest spyder as I've been fighting zscaler and then conda issues and have been completely unable to update anything. It sounds like @dalthviz has reproduced this with the latest version (and yes @dalthviz, I'm already using that workaround), so at least it's not entirely dependent on me overcoming conda issues.

Was this page helpful?
0 / 5 - 0 ratings