Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local modules can't be imported in napari-console #31

Open
psobolewskiPhD opened this issue May 17, 2022 · 0 comments
Open

Local modules can't be imported in napari-console #31

psobolewskiPhD opened this issue May 17, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@psobolewskiPhD
Copy link
Member

psobolewskiPhD commented May 17, 2022

🐛 Bug

In contrast to the python REPL, ipython, VS Code, Jupyter, etc. the napari-console doesn't allow local modules/functions to be imported.
The sys.path in the napari-console does not include "" in sys.path, which is what permits searching of imports from the current dir.
See: https://docs.python.org/3/library/sys.html#sys.path

As a result launching napari from a dir does not permit easy importing of local modules.
Because of the inconsistency with other python tools and the fact that the console so strongly mimics those tools, I consider this a bug. (After some growing pains with the napari-console now I'm a big fan, especially for easy single-window hacking, as well as for demos/screensharing.)

However, as the discussion here on zulip with @Czaki points out, it could be considered a feature: https://napari.zulipchat.com/#narrow/stream/212875-general/topic/napari.20console.20vs.20ipython.3A.20importing.20local.20modules
By blocking local imports, the end user can't mask important "real" modules needed by plugins.

To Reproduce

Steps to reproduce the behavior:

  1. Have a folder, e.g. dev or python_stuffs and a subfolder napari_scripts (or any other such)
  2. Launch napari from terminal in dev or whatever.
  3. Open console and try to import from a script/module in napari_scripts: from napari_scripts.napari_measure import measure_shape

Result:

ModuleNotFoundError: No module named 'napari_scripts'

The broader import from napari_scripts import napari_measure results in the same error, also it doesn't matter if the sub-dir napari_scripts has __init__.py or not.

(FYI if napari is launched directly from the subdirectory with the scripts, they still cannot be imported.)

Edit: to make matters more confusing for the end user, pwd() works correctly and shows the dir. Also, to further blur the lines between ipython and napari-console: they share a history.

Expected behavior

Both of the above imports work in any of the above mentioned python consoles (regardless of __init__.py). As a result, I would expect the import to work in napari-console and my local functions to be available.

Additional Context

The issue with possible local imports, as @Czaki pointed out is that a user with a local file like tensorflow.py will prevent a napari plugin that uses tensorflow from working.

I tested this by making such a file with just a test function that prints a string.
In live napari, stardist-napari (the tensorflow test plugin) works just fine.
If one uses:
import sys; sys.path.insert(0, "") to add "" to the top of sys.path my local imports work, but launching the Stardist plugin an error results, fairly clearly identifying the issue:

ImportError: cannot import name '__version__' from 'tensorflow' (/Users/piotrsobolewski/Dev/python_stuffs/tensorflow.py)

If one uses instead: import sys; sys.path.append('')
then again the local imports work, just like in ipython, et al (which have "" in sys.path at the bottom).
Importantly, Stardist plugin still works despite the "malicious/ignorant" tensorflow.py

So the key is to put "" after all the other search dirs (e.g. conda env ones).
ipython uses a init_path function that does this on start setting sys.path[0]=""

PS: Not sure if this should be here or napari-console repo?

Environment

napari: 0.4.16rc2.dev17+g0c148bb6
Platform: macOS-12.3.1-arm64-arm-64bit
System: MacOS 12.3.1
Python: 3.9.12 | packaged by conda-forge | (main, Mar 24 2022, 23:25:14) [Clang 12.0.1 ]
Qt: 5.15.2
PyQt5: 5.15.4
NumPy: 1.21.5
SciPy: 1.8.0
Dask: 2022.02.0
VisPy: 0.10.0

OpenGL:

  • GL version: 2.1 Metal - 76.3
  • MAX_TEXTURE_SIZE: 16384

Screens:

  • screen 1: resolution 1680x1050, scale 2.0

Plugins:

  • aicsimageio-in-memory: 0.6.1
  • aicsimageio-out-of-memory: 0.6.1
  • clEsperanto: 0.16.0
  • console: 0.0.4
  • napari-accelerated-pixel-and-object-classification: 0.6.8
  • napari-assistant: 0.1.7
  • napari-math: 0.0.1b0
  • napari-ome-zarr: 0.4.0
  • napari-sklearn-decomposition: 0.0.1
  • napari-svg: 0.1.6
  • napari-time-slicer: 0.4.3
  • napari-tools-menu: 0.1.10
  • napari_skimage_regionprops1: 0.4.0
  • napari_skimage_regionprops2: 0.4.0
  • natari: 0.2.7
  • ome-types: 0.2.9
  • scikit-image: 0.4.16rc2.dev17+g0c148bb6
  • stardist-napari: 2022.4.8
@psobolewskiPhD psobolewskiPhD added the bug Something isn't working label May 17, 2022
@psobolewskiPhD psobolewskiPhD transferred this issue from napari/napari Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant