Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass variable between python scripts

Tags:

python

I'm sure this is very simple but I've been unable to get it working correctly. I need to have my main python script call another python script and pass variables from the original script to the script that I've called

So for a simplistic example my first script is,

first.py x = 5 import second 

and my second script is,

second.py  print x 

and I would expect it to print x but I get

NameError: name 'x' is not defined 

I'm not sure if import is right way to achieve this, but if someone could shed light on it in a simple way that would be great!

thanks,


EDIT

After reading the comments I thought I would expand on my question. Aswin Murugesh answer fixes the import problem I was having, however the solution does not have the desired outcome as I can not seem to pass items in a list this way.

In first.py I have a list which I process as follows

for insert, (list) in enumerate(list, start =1):     'call second.py passing current list item' 

I wanted to pass each item in the list to a second python file for further processing (web scraping), I didn't want to do this in first.py as this is meant to be the main 'scan' program which then calls other programs. I hope this now make more sense.

Thanks for the comments thus far.

like image 561
DavidJB Avatar asked Apr 16 '13 22:04

DavidJB


People also ask

How do you share variables between files in Python?

The best way to share global variables across modules across a single program is to create a config module. Just import the config module in all modules of your application; the module then becomes available as a global name.

How do you pass arguments in a Python script?

Many times you need to pass arguments to your Python scripts. Python provides access to these arguments through the sys module. You can directly access the argv functionality and handle the parse of arguments in your own, or you can use some other module as argparse that does that for you.


2 Answers

When you call a script, the calling script can access the namespace of the called script. (In your case, first can access the namespace of second.) However, what you are asking for is the other way around. Your variable is defined in the calling script, and you want the called script to access the caller's namespace.

An answer is already stated in this SO post, in the question itself:

Access namespace of calling module

But I will just explain it here in your context.

To get what you want in your case, start off the called script with the following line:

from __main__ import * 

This allows it to access the namespace (all variables and functions) of the caller script.

So now your calling script is, as before:

x=5 import second 

and the called script is:

from __main__ import * print x 

This should work fine.

like image 98
Abhranil Das Avatar answered Oct 02 '22 15:10

Abhranil Das


use the following script:

first.py:

x=5 

second.py

import first print first.x 

this will print the x value. Always imported script data should be referenced with the script name, like in first.x

like image 25
Aswin Murugesh Avatar answered Oct 02 '22 15:10

Aswin Murugesh