Bug 409306 - no access to local modules/files
Summary: no access to local modules/files
Status: RESOLVED FIXED
Alias: None
Product: cantor
Classification: Applications
Component: python3-backend (show other bugs)
Version: 19.04
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: Cantor Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-06-29 14:01 UTC by avlas
Modified: 2019-06-30 10:21 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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!