nteract / hydrogen

:atom: Run code interactively, inspect data, and plot. All the power of Jupyter kernels, inside your favorite text editor.
https://nteract.gitbooks.io/hydrogen/
MIT License
3.92k stars 334 forks source link

Attribute error importing a user defined module #2092

Closed scoleli closed 3 years ago

scoleli commented 3 years ago

Description

Hi everyone. I am new on GitHub and I signed up with the purpose to ask for help for this tricky problem with hydrogen. I have writtena a python script (a module?) TSP_functions.py with some functions inside. I need to call these functions from other python scripts. When I try to do so with:

import TSP_functions as TSP

and then inside the code

points = TSP.randomGeneratePoints(n, dim, bounds)

the hydrogen run raises this attribute error:

`AttributeError Traceback (most recent call last)

in 9 boundaries = np.array([[0, 10], [0, 10]]) 10 # Generate random points in the n-dim defined region ---> 11 points = TSP_functions.randomGeneratePoints(N, dim, boundaries) 12 # Set display options (print to screen) for a better visualization 13 np.set_printoptions(precision=3, suppress=True) AttributeError: module 'TSP_functions' has no attribute 'randomGeneratePoints'` The tricky point is that when I run the code with the atom `script` plugin, or from windows terminal (powershell), no errors are raised, and the code does what it is intended to do. Another strange thing for me is that pylint lints the import line with error `E0401 Unable to import 'TSP_functions' [pylint]`. I was not able to understand why, but I know that I can fix this last issue simply by ignoring this error. Yet, I do not understand why hydrogen raises the attribute error, whereas the function call works just fine when I run the code from terminal. The python executables are set in system environmental variables and PYTHONPATH (sys.path) contains the path where there is `TSP_functions.py`. All the scripts are in the same directory. I tried also with: `from TSP_functions import *` But the result is quite the same: the function call failed because the function name is not recognized. I really appreciate any help. Thanks. ### Versions OS version: Windows 10 Home > `atom --version` ``` Atom : 1.56.0 Electron: 9.4.4 Chrome : 83.0.4103.122 Node : 12.14.1 ``` > `apm --version` ``` apm 2.5.2 npm 6.14.8 node 12.4.0 x64 atom 1.56.0 python 3.8.5 git 2.30.1.windows.1 visual studio ``` Hydrogen version: 2.15.3 ### Plugins Have you installed and activated any of the Hydrogen plugins below ? - [x] hydrogen-python - [x] Hydrogen Launcher ### Logs Hydrogen: IO message: Message {idents: Array(1), header: {…}, parent_header: {…}, metadata: {…}, content: {…}, …}buffers: []content: {execution_state: "busy"}header: {msg_id: "03aa387f-1bd8b4e8ed6c1e5903939090_110", msg_type: "status", username: "username", session: "03aa387f-1bd8b4e8ed6c1e5903939090", date: "2021-04-17T19:24:08.949474Z", …}idents: [Uint8Array(50)]metadata: {}parent_header: {username: "elias", session: "00000000-0000-0000-0000-000000000000", msg_type: "execute_request", msg_id: "execute_8d6f8116-9307-4f09-8420-aff4e7caf0f0", date: "2021-04-17T19:24:08.947000Z", …}__proto__: Object utils.tsx:186 Hydrogen: IO message: Message {idents: Array(1), header: {…}, parent_header: {…}, metadata: {…}, content: {…}, …} utils.tsx:186 Hydrogen: IO message: Message {idents: Array(1), header: {…}, parent_header: {…}, metadata: {…}, content: {…}, …} utils.tsx:186 Hydrogen: IO message: Message {idents: Array(1), header: {…}, parent_header: {…}, metadata: {…}, content: {…}, …} utils.tsx:186 Hydrogen: watchview running: utils.tsx:186 Hydrogen: shell message: Message {idents: Array(0), header: {…}, parent_header: {…}, metadata: {…}, content: {…}, …}
aminya commented 3 years ago

This seems similar to #1893. Do any of the previous versions of Hydrogen work?

PyLint error means that it can't find TSP_functions. The same thing happens on Hydrogen.

scoleli commented 3 years ago

Thank you for the hint @aminya. I solved the hydrogen issue by updating ipykernel, as mentioned in #1893. On terminal: conda update ipykernel

Pylint still lints the import line, but hydrogen runs the code without raising any attribute or import errors. I am just very satisfied of the result, but if someone have some idea about how to fix also the pylint issue, any contribution will be appreciated .

deadmaze commented 1 year ago

Hi all, i cannot seem to import the sklearn module into my version of hydrogen. @aminya Do you happen to know what I might be able to do in order to actually get sklearn module to be seen by my pykernel?

Thanks!

aminya commented 1 year ago

@NIGHTWAV3 If you source the environment and then open Atom from that shell, it should work

deadmaze commented 1 year ago

@NIGHTWAV3 If you source the environment and then open Atom from that shell, it should work

Thanks! I ended up figuring out that the sklearn folder in my library was NOT named properly, so the import keyword in python didnt catch it!