0

I checked similar topics but could not find what I am looking for. I simply want to call a py script which includes a sqlite code from another py code and also pass macro variables to it. I simply want to assign a macro variable as run_date and pass the values I gave here to another python code I am calling, however I receive this error :

Traceback (most recent call last):
  File "0_Data_Cleaning_Pipeline.py", line 42, in <module>
    weekly_transactions['report_end_date'] = run_date
NameError: name 'run_date' is not defined

# CONNECT DATABASE
import pandas as pd
import os
import subprocess
from sqlalchemy import create_engine
import sqlite3
# connection to database
db = sqlite3.connect('gencodb.db')
# cursor
cursor = db.cursor()
# Libraries

# ENTER THE DATA WEEKS
run_date = '22/02/2020'

subprocess.call(" python 0_Data_Cleaning_Pipeline.py", shell=True)
2
  • what do you mean by a macro variable? and how are you calling the another python code? via the terminal or inside this code as a module? Commented Feb 29, 2020 at 16:19
  • I want to pass an argument to another code such as run_date. I have this code such as weekly_transactions['report_end_date'] = run_date in the second code and I want to control it from a master code like I copied above. I am running it in atom editor Commented Feb 29, 2020 at 16:23

1 Answer 1

1

You can pass run date as a argument to the program you are calling with subprocess.call like so

subprocess.call("python 0_Data_Cleaning_Pipeline.py".split() + [run_date], shell=True)

and in 0_Data_Cleaning_Pipeline.py you can access the passed argument with

import sys

run_date = sys.argv[1]
Sign up to request clarification or add additional context in comments.

8 Comments

I should copy this into the 0_Data_Cleaning_Pipeline code right? I tried it but did not work. import sys run_date = sys.argv[1]
yes, copy that into the other file. When you say something does not work, please also provide the errors that you get.
I changed run_date = sys.argv[1] to run_date = sys.argv[0] and it works now! However I have a few other arguments I want to pass, how can I add them by using the same syntax you showed above?
you add them with the list with run_date like [run_date, varA, varB] and so on, but make sure they are string or atleast castable to str. then you can access them sys.argv[1], sys.argv[2] and so on
its is weird that you are getting the parameter at sys.argv[0]. may I ask what OS are you using and the python version?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.