如何在PyCharm中无需print函数输出结果,实现Colab同款效果?
Hey there! Great question—this is such a common annoyance when bouncing between Colab’s notebook-style environment and PyCharm’s traditional script setup. Let me walk you through a few straightforward ways to get that "no-print-required" output behavior in PyCharm:
PyCharm has a built-in interactive Python console that works just like Colab’s code cells—any expression you run will automatically return its value without needing print().
- You can open it directly by clicking the Python Console tab at the bottom of the IDE.
- If you want to run an entire script in this mode, right-click your
.pyfile and select Run File in Python Console. All the expressions in your script will output their results automatically, just like in Colab.
If you prefer using the regular "Run" button instead of right-clicking each time, tweak your run settings:
- Click the run configuration dropdown in the top-right corner of PyCharm (it usually shows your script name) and select Edit Configurations.
- In the window that pops up, select your Python script’s configuration.
- Go to the Execution tab and check the box that says Run with Python console.
- Save the configuration. Now every time you click "Run", your script will execute in the interactive console, and expressions will auto-output their results.
If you love Colab’s notebook interface, PyCharm fully supports Jupyter Notebooks.
- Create a new Jupyter Notebook file (File → New → Jupyter Notebook).
- Write your code in cells, hit shift+enter to run each cell, and you’ll get automatic output exactly like Colab—no
print()needed at all.
If you need to output something in a regular script without switching modes, you can use IPython’s display utility:
from IPython.display import display display(df.Quantity.dtypes)
This will render the result nicely, just like the interactive console does. It’s not as seamless as the first three methods, but it’s handy for one-off cases.
Just a quick note: By default, PyCharm runs regular .py scripts in a non-interactive shell, which is why you need print() to see output. Switching to any of the interactive modes above mimics Colab’s behavior perfectly.
内容的提问来源于stack exchange,提问作者Blue Sea

