Bug 409306

Summary: no access to local modules/files
Product: [Applications] cantor Reporter: avlas <jsardid>
Component: python3-backendAssignee: Cantor Bugs <cantor-bugs>
Status: RESOLVED FIXED    
Severity: normal CC: warquark
Priority: NOR    
Version First Reported In: 19.04   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description avlas 2019-06-29 14:01:17 UTC
- I created a file called test_cantor.py and its only content is "print('Hello World!')"

- All the following instructions work in Python 3 but fail in Cantor:

import test_cantor

import importlib
importlib.import_module('test_cantor')

import importlib
importlib.reload('test_cantor')

import runpy
runpy.run_module(mod_name='test_cantor')

import runpy
runpy.run_path('test_cantor.py')

- This bug can be workarounded in Cantor (with the exception of last instruction that continues triggering an error) if I manually append the local directory to sys.path:

import sys

flag_append_local_dir = True
for path in sys.path:
    if path == '.':
        flag_append_local_dir = False
        break

if flag_append_local_dir:
    sys.path.append('.')

for path in sys.path:
    print(path)

- But this is not necessary in Python 3, so I think that Cantor is internally moving to a different folder and can't see local modules/files. 

- Could you please try to reproduce the issue and hopefully fix it?

- Please note that the error for "runpy.run_path('test_cantor.py')" seems different as it cannot be workarounded. This is the output I get for the case it is useful to fix this second issue:

Traceback (most recent call last):
  File "/home/username/test_cantor.cws", line 1, in <module>
  File "/home/miniconda/envs/cenv1/lib/python3.7/runpy.py", line 263, in run_path    pkg_name=pkg_name, script_name=fname)
  File "/home/miniconda/envs/cenv1/lib/python3.7/runpy.py", line 93, in _run_module_code
    with _TempModule(mod_name) as temp_module, _ModifiedArgv0(fname):
  File "/home/miniconda/envs/cenv1/lib/python3.7/runpy.py", line 54, in __enter__
    self._saved_value = sys.argv[0]
IndexError: list index out of range
Comment 1 Nikita Sirgienko 2019-06-30 09:23:07 UTC
That is 'local' directory?
If f I run python interpreter, local directory is a directory in which I run interpreter.
There is similar logic for python scripts.

But in case Cantor, it is more comlex. Cantor could be run, for example, from desktop icon or from OS menu. In cases like this, which directory should be local for python backend?

But you offered a good suggestion, I have already added support local modules for loaded worksheets.
Comment 2 avlas 2019-06-30 10:21:44 UTC
It worked (even for the last instruction).
Thank you!