Skip to content

Commit b375677

Browse files
authored
Create Python能否在保存程序變量情況下啟動控制台.md
1 parent d872582 commit b375677

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Python 能否在保存程序變量情況下啟動控制台
2+
3+
## 問題
4+
5+
類似於 matlab 可以在程序完成後直接在控制台操作變量,而不是啟動一個獨立的控制台程序,不知道有沒有哪款 Python 的 ide 支持這種行為
6+
7+
問題出自 [segmentfault](https://segmentfault.com/q/1010000005897684/a-1020000005906439), by [mzmssg](https://segmentfault.com/u/mzmssg)
8+
9+
## 回答
10+
11+
Python 自帶的 IDLE 就可以做到了,你開啟一個 python file 後 run module,你會發現主控台上可以操控 file 中的 variable
12+
13+
-----
14+
15+
Pycharm 的部分我自己試了一下,你進到 `Run/Edit Configurations...`
16+
17+
![图片描述][1]
18+
19+
然後把 `Interpreter options` 加入 `-i` 的選項:
20+
21+
![图片描述][2]
22+
23+
之後運行 script 完畢,shell 會 keep 在那邊不會結束
24+
25+
-----
26+
27+
其實不需要 ide 就可以做到你想做的了
28+
29+
假設你有一個 python script `test.py`
30+
31+
```python
32+
a = 5
33+
b = [1, 2, 3]
34+
```
35+
36+
直接用:
37+
38+
```
39+
$ python -i test.py
40+
```
41+
42+
運行 `test.py` 完畢後,Python 會停在 console 中可以繼續互動
43+
44+
或是用:
45+
46+
```
47+
$ python
48+
```
49+
50+
開啟 python shell 後,用 `import` 匯入 test 並執行,接著你就可以操控 variable 了:
51+
52+
```
53+
>>> from test import *
54+
>>> a
55+
5
56+
>>> b
57+
[1, 2, 3]
58+
```
59+
60+
這也有同樣效果
61+
62+
[1]: https://segmentfault.com/img/bVyYF6
63+
[2]: https://segmentfault.com/img/bVyYFI

0 commit comments

Comments
 (0)