Ранее мы уже говорили о том, как выполнить какой-либо код перед открытием интерактивной консоли.
Расскажу еще один способ! На самом деле, даже запустив интерпретатор в обычном режиме с выполнением скрипта из файла вы можете в любом месте активировать интерактивный режим. Или даже несколько по очереди. За это отвечает модуль code.
Как это может пригодиться?
🔸 Вам не хватает pdb и хочется больше "власти"
🔸 Нужно запросить у юзера данные в достаточно сложном виде. В этом случае можно попросить его создать что ему надо и сохранить в определённую переменную, с которой потом и работать.
🔸 Нужна изолированная среда для выполнения каких-либо действий.
🔸 Просто забавы ради😁
Запускается консоль очень просто
import code
ic = code.InteractiveConsole()
try:
ic.interact()
except SystemExit:
pass
Выход обратно на предыдущий уровень происходит как обычно, вызов функции exit() или клавиши Ctrl+D(Ctrl+Z для Windows).
Я набросал простой пример с некоторым функционалом
🔹 Меняются символы приглашения
🔹 В неймспейсы добавляются дополнительные объекты
🔹 Считается время, проведённое в интерактивном режиме
🔹 Скрипт просит юзера заполнить переменную и по выходу распечатывает её значение
Код здесь ↗️
Для запуска сохраните код в файл и запускайте как обычный скрипт.
python my_console.py
Для быстрого запуска можно сделать отдельный алиас
alias py="python3 /home/username/my_console.py"
#tricks#source
🆓REDEEM CODES🆓
🎁🚀AFK Arena Redeem #Codes
💫📌#January 2026
🌟🎁New Code:【 vdj82fht4r 】
💎💎💎Redeem now for 3,000 diamonds, ten time emblems, ten stargazer scrolls, and ten common scrolls!!!
👀All the Active AFK Arena Codes.
Add @APKFreeDownloader_Bot to get any apk you want.
🆓REDEEM CODES🆓
🎁🚀AFK Arena #Codes | #January 2026
💫🌟4 New Codes Released This Week!
👉Code 1:【 DON2026classic 】
👉Code 2:【 ujqrukd2at 】
👉Code 3:【 uj5fs5z58s 】
👉Code 4:【 u4fctemje2 】
🎁💎Redeem now for 2.2k diamond any many more!!!
🔗All working AFK Arena codes.
Add @APKFreeDownloader_Bot to get any apk you want.
Wikipedia每日一图(#January 31, 2026)https://ift.tt/129ov6E
Isambard Kingdom Brunel standing before the launching chains of the SS Great Eastern. The ship was launched on this day in 1858.. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#January 30, 2026)https://ift.tt/Z0TVbvJ
Lake Baikal in winter. Ice ridges near Olkhon island in Pribaikalsky National Park.. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#January 29, 2026)https://ift.tt/YMrOJT0
Great Cormorant (Phalacrocorax carbo), Little Egret (Egretta garzetta) and Gadwall (Mareca strepera) in misty Taudaha Lake during winter, near Kathmandu, Nepal.. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#January 28, 2026)https://ift.tt/2EM6iKC
Sunset over the ice of Brofjorden in Sandvik, Brastad, Lysekil Municipality, Sweden. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#January 27, 2026)https://ift.tt/Msrmcyu
“Selection” of Hungarian Jews on the ramp at Auschwitz-II-Birkenau in German-occupied Poland, May/June 1944, during the final phase of the Holocaust. Jews were sent either to forced labor or the gas chambers. 81 years ago today, on January 27, 1945, Auschwitz was liberated by the Red Army. Today is also International Holocaust Remembrance Day.. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#January 26, 2026)https://ift.tt/EsDR49p
Apple orchards after snowfall in Sangla in the northern Indian state of Himachal Pradesh. India has the second largest area under apple cultivation in the world and is the fifth largest producer of the fruit globally. More than 90% of India's apple production comes from just two of its northern Himalayan states: Jammu and Kashmir, and Himachal Pradesh. Today is Republic Day in India.. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#January 25, 2026)https://ift.tt/JzivhFB
Window in a former joinery workshop in Beilstein, Germany. Hop has grown into the workshop and has draped its tendrils before this window.. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#January 24, 2026)https://ift.tt/uP9JQ1U
The entrance of the locomotive Hrvatske Željeznice class 2044 with a local train to Zagreb between Turčin and Sveti Ilija, Croatia. July 2009. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#January 23, 2026)https://ift.tt/oNaVYGk
Northwest view of Yugar village and fields, viewed from Phuktal Gompa. Elevation 3,850 m (12,630 ft)), on the left bank of the Tsarap River (elev. 3,820 m (12,530 ft)). In this arid terrain above the tree line, villagers rely on water from the snow-fed Phu Nala (flowing from centre to bottom) for their fields. Zanskar, Ladakh, India. 了解更多 . #Wikipedia每日一图
Wikipedia每日一图(#January 22, 2026)https://ift.tt/4AIKm1L
Aerial view of 2023 Bishwa Ijtema in Bangladesh. With 5 million adherents, Bishwa Ijtema is the second-largest Muslim gathering in the world.. 了解更多 . #Wikipedia每日一图