Here is a minimal example:
I created and activate a virtual environment:
~/BacSable/bac2 $ mkvirtualenv minimal_env
created virtual environment CPython3.10.5.final.0-64 in 651ms
creator CPython3Posix(dest=/Users/raoul/.virtualenvs/minimal_env, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/raoul/Library/Application Support/virtualenv)
added seed packages: pip==22.1.2, setuptools==62.6.0, wheel==0.37.1
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
My script minimal.py
contains:
print("yes I can print")
my_input = input("Do you have a problem? (copy paste your answer)")
print(my_input)
my_input = input("Do you have a problem? (type your answer)")
print(my_input)
Completion works well with LSP in the script buffer.
With C-c C-z
I can switch from script to console and vice-versa.
With C-c C-c
I run the script and I encounter this problem:
The input works if I copy-paste to enter an answer. However, if I type it, I get an error:
Python 3.10.5 (v3.10.5:f377153967, Jun 6 2022, 12:36:10) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("yes I can print")
... my_input = input("Do you have a problem? (copy paste your answer)")
... print(my_input)
... my_input = input("Do you have a problem? (type your answer)")
... print(my_input)
yes I can print
Do you have a problem? (copy paste your answer)yes
yes
Do you have a problem? (type your answer)yes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'yes' is not defined
>>> print(my_input)
__PYTHON_EL_eval("\ndef __PYTHON_EL_get_completions(text):\n completions = []\n completer = None\n\n try:\n import readline\n\n try:\n import __builtin__\n except ImportError:\n # Python 3\n import builtins as __builtin__\n builtins = dir(__builtin__)\n\n is_ipython = ('__IPYTHON__' in builtins or\n '__IPYTHON__active' in builtins)\n splits = text.split()\n is_module = splits and splits[0] in ('from', 'import')\n\n if is_ipython and is_module:\n from IPython.core.completerlib import module_completion\n completions = module_completion(text.strip())\n elif is_ipython and '__IP' in builtins:\n completions = __IP.complete(text)\n elif is_ipython and 'get_ipython' in builtins:\n completions = get_ipython().Completer.all_completions(text)\n else:\n # Try to reuse current completer.\n completer = readline.get_completer()\n if not completer:\n # importing rlcompleter sets the completer, use it as a\n # last resort to avoid breaking customizations.\n import rlcompleter\n completer = readline.get_completer()\n if getattr(completer, 'PYTHON_EL_WRAPPED', False):\n completer.print_mode = False\n i = 0\n while True:\n completion = completer(text, i)\n if not completion:\n break\n i += 1\n completions.append(completion)\n except:\n pass\n finally:\n if getattr(completer, 'PYTHON_EL_WRAPPED', False):\n completer.print_mode = True\n return completions\nprint(';'.join(__PYTHON_EL_get_completions(\"y\")))", "<string>")
>>>
The interpreter behaves as if I typed >>>yes
directly (instead of just passing it to input()
):
>>> yes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'yes' is not defined
>>>
Of course I can execute the script from eshell:
Welcome to the Emacs shell
~/BacSable/bac2 $ python minimal.py
yes I can print
Do you have a problem? (copy paste your answer)yes
yes
Do you have a problem? (type your answer)yes
yes
~/BacSable/bac2 $
Or
~/BacSable/bac2 $ python -i minimal.py
yes I can print
Do you have a problem? (copy paste your answer)yes
yes
yes
Do you have a problem? (type your answer)yes
yes
yes
>>> my_input
my_input
'yes'
>>>
But is there a way to make reading input work by running the script with C-c C-c
?