scotscotmcc
scotscotmcc

Reputation: 3123

How to open interactive python terminal with initial code?

Is there any way to open an active, interactive python terminal that will automatically run my initial ~10 lines of code?

My specific use (which will probably be needed to clarify the question) is that I have a Microsoft SQL Server that it takes just a little too long to bother opening SSMS when I just want to quickly find the ID for a record or something.

What I've found myself doing is opening a python terminal (just have python pinned to the start menu), and copy-pasting in ~10 lines of code from a text file I leave open (import sqlalchemy, import pandas, set driver and connection string). Then I'll actually type in whatever I'm looking for (pd.read_sql('Select * from table where x = y') and getting the name of my record.

I've no doubt that there are plenty of completely better solutions to viewing data in a database effectively, but I'm hoping that I can easily improve my current (albeit janky) worthflow until I get around to a real solution. Plus, just seems like a useful thing to do.

Upvotes: 0

Views: 155

Answers (1)

Chris
Chris

Reputation: 137179

You could use python -i script_with_first_10_lines.py:

When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This can be done by passing -i before the script.

This will run your script, then stay in interactive mode so you can run more code by typing it in.

Upvotes: 1

Related Questions