Python Forum
Calling Variables from Other Files in Different Folders
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling Variables from Other Files in Different Folders
#11
PYTHONPATH does not get defined when you install Python, you need to define it yourself.
Reply
#12
So I would just create a new user variable with name PYTHONPATH and the directory for the value?
Reply
#13
(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.
Reply
#14
(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:

Error:
Traceback (most recent call last): File "F:/Investments/Python/Projects/GoldData/data.py", line 46, in <module> from EconomicData.US_data import US_mb File "F:\Investments\Python\Projects\EconomicData\US_data.py", line 41, in <module> US_trade = pd.read_excel('US Trade.xlsx',index_col='Date') File "F:\Investments\Python\Projects\GoldData\venv\lib\site-packages\pandas\util\_decorators.py", line 296, in wrapper return func(*args, **kwargs) File "F:\Investments\Python\Projects\GoldData\venv\lib\site-packages\pandas\io\excel\_base.py", line 304, in read_excel io = ExcelFile(io, engine=engine) File "F:\Investments\Python\Projects\GoldData\venv\lib\site-packages\pandas\io\excel\_base.py", line 867, in __init__ self._reader = self._engines[engine](self._io) File "F:\Investments\Python\Projects\GoldData\venv\lib\site-packages\pandas\io\excel\_xlrd.py", line 22, in __init__ super().__init__(filepath_or_buffer) File "F:\Investments\Python\Projects\GoldData\venv\lib\site-packages\pandas\io\excel\_base.py", line 353, in __init__ self.book = self.load_workbook(filepath_or_buffer) File "F:\Investments\Python\Projects\GoldData\venv\lib\site-packages\pandas\io\excel\_xlrd.py", line 37, in load_workbook return open_workbook(filepath_or_buffer) File "F:\Investments\Python\Projects\GoldData\venv\lib\site-packages\xlrd\__init__.py", line 111, in open_workbook with open(filename, "rb") as f: FileNotFoundError: [Errno 2] No such file or directory: 'US Trade.xlsx' Process finished with exit code 1
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.py

import 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
Reply
#15
PYTHONPATH only tells Python where to look for modules (.py files). It does not add a search path for file open.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create new folders and copy files cocobolli 3 1,337 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 954 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  using variables with functions imported from different files. Scordomaniac 3 1,221 May-24-2022, 10:53 AM
Last Post: deanhystad
Question Calling on a Variable using other Variables jacknewport 4 1,956 Jul-23-2021, 04:18 PM
Last Post: jacknewport
  Moving files to Folders giddyhead 13 9,006 Mar-07-2021, 02:50 AM
Last Post: giddyhead
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,835 Dec-06-2020, 04:14 PM
Last Post: Divya577
  How do use data from csv files as variables? JUSS1K 1 2,096 Oct-25-2020, 08:31 PM
Last Post: GOTO10
  Passing Variables between files. victorTJ 3 2,223 Oct-17-2020, 01:45 AM
Last Post: snippsat
  sub-folders in folders from text line jenost 1 1,533 Mar-31-2020, 07:16 AM
Last Post: ndc85430
  Accessing files in various directories and folders ccuny 2 2,112 May-08-2019, 12:11 PM
Last Post: ccuny

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020