JPC
JPC

Reputation: 5173

ipython to run a file before open in each session

Is there a way to have ipython to run a config file where I have my global variable written in there ? So we have multiple servers with different user name and password and frequent module that I am using. Is there a way to put that in a filer prior to open python session ? I'm in windows and new to ipython.

from pandas import * 
import numpy as np
import pyodbc 

cursor = connect() 

user1 = foo
pwd1 = xx1

user2 = foo2

So each time that I open a session in ipython it would already have this code run.

Upvotes: 1

Views: 90

Answers (1)

Marius
Marius

Reputation: 60160

You can make a macro called something like server_setup:

In [1]: from pandas import * 
   ...: import numpy as np
   ...: 
   ...: user1 = 'foo'
   ...: pwd1 = 'xx1'
   ...: 
   ...: user2 = 'foo2'

In [2]: %macro server_setup 1
Macro `server_setup` created. To execute, type its name (without quotes).

Store it:

%store server_setup
Stored 'server_setup' (Macro)

Then set

c.StoreMagics.autorestore = True

in your IPython profile. Every time you open a new IPython session, just type server_setup and it'll run the setup code.

Upvotes: 1

Related Questions