当 运行 python 脚本使用指定 conda 环境的 shebang 时,来自 input() 的 EOL 错误
EOL error from input() when running python script using shebang that specifies a conda environment
我正在制作一个 python CLI,提示用户在其 运行 期间的特定时间输入。我想为此使用输入函数,但在读取一行到达 input() 行时得到 EOFError: EOF。
我已经进行了一些测试,我认为问题在于我在脚本上使用了 shebang,使其 运行 具有特定的公寓环境。这是重现问题所需的最少代码
#!/usr/bin/env conda run -n TEST python
def main(): # testing
chromatogram_result = input()
print(chromatogram_result)
if __name__ == "__main__":
main()
其中 TEST 是我的 conda 环境名称。
如果我 运行 通过简单地调用文件名从命令行执行此操作,则会出现此问题
$ ./my_file.py
如果我从终端使用基于 shebang
的命令 运行 它也会发生
$ conda run -n TEST python my_file.py
但如果我激活正确的 conda 环境然后 运行 它就不会发生
$ conda activate TEST
$ python3 my_file.py
我的问题是为什么会发生这种情况,是否有更好的方法在 shebang 中调用特定的 conda 环境来避免这种情况?或者我是否必须使用普通的 shebang 并记得在每次我想使用脚本时激活我的环境?
这是在 macOS 10.14.6 上,使用 conda 4.10.3。它似乎并不特定于一个 conda 环境,我在我的系统上尝试了几个不同的环境,并且都在这个 shebang
中给出了相同的错误
默认情况下,Conda 在 conda run
上缓冲 I/O。您需要 --no-capture-output
标志才能进行交互 I/O:
#!/usr/bin/env conda run -n TEST --no-capture-output python
def main(): # testing
chromatogram_result = input()
print(chromatogram_result)
if __name__ == "__main__":
main()
至于是否有更好的方法:不,这是我见过的创建在特定 Conda 环境中评估的可执行 Python 脚本的最佳模式。
我正在制作一个 python CLI,提示用户在其 运行 期间的特定时间输入。我想为此使用输入函数,但在读取一行到达 input() 行时得到 EOFError: EOF。
我已经进行了一些测试,我认为问题在于我在脚本上使用了 shebang,使其 运行 具有特定的公寓环境。这是重现问题所需的最少代码
#!/usr/bin/env conda run -n TEST python
def main(): # testing
chromatogram_result = input()
print(chromatogram_result)
if __name__ == "__main__":
main()
其中 TEST 是我的 conda 环境名称。
如果我 运行 通过简单地调用文件名从命令行执行此操作,则会出现此问题
$ ./my_file.py
如果我从终端使用基于 shebang
的命令 运行 它也会发生$ conda run -n TEST python my_file.py
但如果我激活正确的 conda 环境然后 运行 它就不会发生
$ conda activate TEST
$ python3 my_file.py
我的问题是为什么会发生这种情况,是否有更好的方法在 shebang 中调用特定的 conda 环境来避免这种情况?或者我是否必须使用普通的 shebang 并记得在每次我想使用脚本时激活我的环境?
这是在 macOS 10.14.6 上,使用 conda 4.10.3。它似乎并不特定于一个 conda 环境,我在我的系统上尝试了几个不同的环境,并且都在这个 shebang
中给出了相同的错误默认情况下,Conda 在 conda run
上缓冲 I/O。您需要 --no-capture-output
标志才能进行交互 I/O:
#!/usr/bin/env conda run -n TEST --no-capture-output python
def main(): # testing
chromatogram_result = input()
print(chromatogram_result)
if __name__ == "__main__":
main()
至于是否有更好的方法:不,这是我见过的创建在特定 Conda 环境中评估的可执行 Python 脚本的最佳模式。