Skip to content

Commit 8703791

Browse files
committed
cli: Allow importing modules from the current directory when running a command or a file.
Matches python behavior, and fixes another regression with respect to the bash script Fixes sagemath#40908
1 parent 340d6f6 commit 8703791

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/sage/cli/eval_cmd.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from sage.cli.options import CliOptions
44
from sage.repl.preparse import preparse
55
from sage.all import sage_globals
6+
import os, sys
67

78

89
class EvalCmd:
@@ -36,6 +37,9 @@ def run(self) -> int:
3637
r"""
3738
Execute the given command.
3839
"""
40+
# Allow importing modules from the current directory, matching python behavior
41+
sys.path.append(os.getcwd())
42+
3943
code = preparse(self.options.command)
4044
eval(compile(code, "<cmdline>", "exec"), sage_globals())
4145
return 0

src/sage/cli/run_file_cmd.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from sage.repl.load import load_cython
66
from sage.misc.temporary_file import tmp_filename
77
from sage.all import sage_globals
8+
import os, sys
89

910

1011
class RunFileCmd:
@@ -40,6 +41,9 @@ def run(self) -> int:
4041
r"""
4142
Execute the given command.
4243
"""
44+
# Allow importing modules from the current directory, matching python behavior
45+
sys.path.append(os.getcwd())
46+
4347
input_file = self.options.file[0]
4448
if input_file.endswith('.sage'):
4549
input_file = str(preparse_file_named(input_file))

0 commit comments

Comments
 (0)