gpt4 book ai didi

python - 如何让 'python -i' 读取 PYTHONSTARTUP 文件?

转载 作者:行者123 更新时间:2023-12-03 22:30:11 26 4
gpt4 key购买 nike

我正在运行 Python 3.7.5。

当我开发一个程序 myprog.py 时,我经常运行

$ python3 -i myprog.py

这让我可以交互地测试功能,这非常有用。

不幸的是,如 Python documentation 中所述, python3 -i 不会读取 PYTHONSTARTUP 环境变量指向的文件:

-i

When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command, even when sys.stdin does not appear to be a terminal. The PYTHONSTARTUP file is not read.



这意味着我在 PYTHONSTARTUP 文件中定义的所有我最喜欢的导入和辅助函数都不可用。

有没有一些实用的方法来解决这个问题?我发现的最好的解决方法是运行
$ python3
>>> from myprog import *
>>>

而不是 python3 -i myprog.py 。然而,每次我修改 myprog.py 并重新启动 Python session 时,这都会涉及更多的输入。有没有更好的办法?

最佳答案

如果您使用的是 Windows,只需键入

py myprog.py

python myprog.py

它将运行简单,您无需重新启动即可修改代码

不要忘记将python放入您的路径中

就像如果你使用 git path 或 cmd with atom 只需按照这个

mkdir 新文件夹名
cd 新文件夹名
ls 新文件夹名
myprog.py #should 包含你 Python_file

然后输入“原子”。如果你有 dot 。或者你的 IDE 然后输入

py myprog.py
或者
python myprog.py

你会得到结果

关于python - 如何让 'python -i' 读取 PYTHONSTARTUP 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58889721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com