Jul-31-2020, 05:49 PM
PYTHONPATH does not get defined when you install Python, you need to define it yourself.
Calling Variables from Other Files in Different Folders
|
Jul-31-2020, 05:49 PM
PYTHONPATH does not get defined when you install Python, you need to define it yourself.
Jul-31-2020, 05:54 PM
So I would just create a new user variable with name PYTHONPATH and the directory for the value?
(Jul-31-2020, 05:23 PM)ndc85430 Wrote: As an aside, do you still need the empty __init__.py for Python 3? I thought it was only needed for 2.Yes from Python 3.3 Implicit Namespace Packages are allowed without an __init__.py file.A little more about in this post. Doing it the way in this Thread with PYTHONPATH ,i have had some trouble not using __init__.py in folders.I usually don't do it this way,have usually one __init__.py in top level folder folder lifting sub-modules.This to avoid long import statement(that i do not like) for users of package.
Jul-31-2020, 10:48 PM
(Jul-31-2020, 05:54 PM)illmattic Wrote: So I would just create a new user variable with name PYTHONPATH and the directory for the value? Alright, well this did the trick but other problems have arisen. so in my current working file (F:\Python\Projects\WorkingFile) I write 'from EconomicData.US_data import US_mb' hoping to get the 'US_mb' variable from F:\Python\Projects\EconomicData but this error pops up: It's confusing cause this excel file is related to a different variable but to find a solution I add the 'US Trade.xlsx' file into the current directory and I am able to get the 'US_mb' variable. Can someone tell me why this is and how I can fix it? This is the code from US_data.pyimport pandas as pd from functions import data_extract from functions import combine_data from functions import data_extract_monthlysum pce_inflation = data_extract('FRED/PCEPILFE', 'Core_PCE', 'yes',3) cpi_inflation = data_extract('FRED/CPIAUCNS', 'CPI', 'yes',3) core_cpi = data_extract('FRED/CPILFENS', 'Core_CPI', 'yes', 3) ppi = data_extract('FRED/WPUFD49207', 'PPI', 'yes', 3) core_ppi = data_extract('FRED/WPUFD4131', 'Core_PPI', 'yes', 3) five_year = data_extract('FRED/T5YIFRM', '5yr-5yr_Forward_Inflation_Expectation','no',1) import_inflation = data_extract('FRED/IR', 'Import_Price_Inflation', 'yes', 3) US_inflation = combine_data([pce_inflation,cpi_inflation,core_cpi,ppi,core_ppi,five_year,import_inflation]) US_m2 = data_extract('FRED/M2','M2','yes',0) US_m1 = data_extract('FRED/M1','M1','yes',1) US_mb = data_extract('FRED/BOGMBASEW', 'Monetary_Base','yes',0) US_comm_loans = data_extract('FRED/CILDCBM027NBOG', 'Commercial&Industrial_Loans','yes',1) US_cons_loans = data_extract('FRED/CLSACBW027NBOG', 'Consumer_Loans','yes',1) US_bank_credit = data_extract('FRED/LOANINVNSA', 'Bank_Credit','yes',1) US_total_loans = US_comm_loans['Commercial&Industrial_Loans'] + US_cons_loans['Consumer_Loans'] US_CI_per_total = US_comm_loans['Commercial&Industrial_Loans'] / US_bank_credit['Bank_Credit']*100 US_ms_credit = combine_data([US_mb,US_m1,US_m2,US_comm_loans,US_cons_loans,US_bank_credit]) US_ms_credit['Total_Loans'] = US_total_loans US_ms_credit['Total_Loans_YY'] = US_total_loans.pct_change(12)*100 US_ms_credit['Total_Loans_YY'] = US_ms_credit['Total_Loans_YY'].round(decimals=1) US_ms_credit['CI_Loans_of_Total_Credit'] = US_CI_per_total US_ms_credit['CI_Loans_of_Total_Credit'] = US_ms_credit['CI_Loans_of_Total_Credit'].round(decimals=1) US_ind_prod = data_extract('FRED/INDPRO', 'Industrial_Production_Index', 'yes',3) US_neworders = data_extract('FRED/NEWORDER', 'New_Goods_Orders_NonDefExAir', 'yes',0) US_new_housing_permits = data_extract('FRED/PERMITNSA', 'New_Housing_Permits', 'yes',0) US_trade = pd.read_excel('US Trade.xlsx',index_col='Date') US_industry = combine_data([US_ind_prod,US_neworders,US_new_housing_permits, US_trade]) US_rpincome = data_extract('FRED/RPI', 'Real_Personal_Income', 'yes',0) US_rpi_ex = data_extract('FRED/W875RX1', 'RPC_ex_transfer_payments', 'yes',0) US_real_disincome = data_extract('FRED/DSPIC96', 'Real_Disposable_Income', 'yes',0) US_real_pce = data_extract('FRED/PCEC96', 'Real_Personal_Consumption_Expenditures', 'yes',0) US_savings_rate = data_extract('FRED/PSAVERT','Savings_Rate','no',1) US_retailSales_exfood = data_extract('FRED/RSXFS', 'Retail_Sales_ExFood','yes',0) US_initial_claims = data_extract_monthlysum('FRED/ICNSA','Initial_Claims', 'yes',0) US_continued_claims = data_extract('FRED/CCNSA', 'Continued_Claims', 'yes', 0) US_consumer = (combine_data([US_rpincome,US_rpi_ex,US_real_disincome,US_real_pce,US_savings_rate,US_retailSales_exfood,US_initial_claims,US_continued_claims])) US_total_data = combine_data([US_consumer,US_industry,US_inflation,US_ms_credit])And here are the functions that I use from a different file: def data_extract(code, column_name, YY, decimals): api_key = open('quandlapikey.txt', 'r').read() data = quandl.get(code, authtoken=api_key, start_date='2000-01-01', collapse='monthly') pd.options.display.float_format = '{:,}'.format data = data.round(decimals=decimals) data.rename(columns={data.columns[0]: column_name}, inplace=True) if YY =='yes': data[column_name + '_YY'] = data[column_name].pct_change(12)*100 data[column_name + '_YY'] = data[column_name + '_YY'].round(decimals=1) return data else: return data def combine_data(df_merged): df_name = reduce(lambda left,right: pd.merge(left,right,on='Date', how='outer'), df_merged).fillna(method='ffill') return(df_name) def add_YY(value,column_name): data = value.pct_change(12) * 100 data = data.round(decimals=1) value[column_name] = data
Aug-01-2020, 07:02 PM
PYTHONPATH only tells Python where to look for modules (.py files). It does not add a search path for file open.
|
|