Python Forum
Calling Variables from Other Files in Different Folders - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Calling Variables from Other Files in Different Folders (/thread-28703.html)

Pages: 1 2


Calling Variables from Other Files in Different Folders - illmattic - Jul-30-2020

I'm currently working on a file in the following directory: F:\Python\Projects\Analysis

And I want to call variables and functions from file: US_data.py in directory: F:\Python\Projects\Economic Data

I've search around to find a way to do this and keep coming back to the idea of creating a new path like this...

sys.path.insert(1, 'F:\Python\Projects\Economic Data'
But this and everything else I've tried isn't working so I am here hoping to find an answer.


Any help would be appreciated.


RE: Calling Variables from Other Files in Different Folders - Axel_Erfurt - Jul-30-2020

copy US_data.py to the F:\Python\Projects\Analysis, then you can use

import US_data



RE: Calling Variables from Other Files in Different Folders - deanhystad - Jul-30-2020

The easiest way to import you own modules is place the module (file) in the same directory as the module (file) that wants to use it. If the module is in another directory you have two choices; specify the path or add the directory of the file you want to import to the Python search path.

Adding paths to the Python search path can be done by modifying the path as you suggest, but if you do all your Python work in a subdirectory of F:\Python\Projects, add that to you python search path and an empty file named __init__.py files, to F:\Python\Projects and all of it's subdirectories.


RE: Calling Variables from Other Files in Different Folders - illmattic - Jul-31-2020

(Jul-30-2020, 10:51 PM)deanhystad Wrote: ... but if you do all your Python work in a subdirectory of F:\Python\Projects, add that to you python search path and an empty file named __init__.py files, to F:\Python\Projects and all of it's subdirectories.

This makes most sense for me as I will have multiple subdirectories under F:\Python\Projects where I'll do all my work and would love the ability to easily pull variables and functions from different subdirectories.

But forgive me, I'm quite new at Python, I'm not sure I follow your instructions. As I understand it, I need to do the following:

1. in my current working file i need to write the code: sys.path.insert(0, 'F:\Python\Projects')
2. create a python file called '__init__.py'
3. Have this file in all of my subdirectories

Is this correct?


RE: Calling Variables from Other Files in Different Folders - deanhystad - Jul-31-2020

If you always do your work under F:\Python\Projects you might want to modify the PYTHONPATH environment variable to include that directory. Then you wouldn't have to modify sys.path directly, it would already contain F:\Python\Projects.


RE: Calling Variables from Other Files in Different Folders - snippsat - Jul-31-2020

Also if this is as mention bye @deanhystad a folder your Python project,then can use site module and not mess with PYTHONPATH/enviroment Path for this.
In this post i explain usage.


RE: Calling Variables from Other Files in Different Folders - illmattic - Jul-31-2020

(Jul-31-2020, 12:45 PM)deanhystad Wrote: If you always do your work under F:\Python\Projects you might want to modify the PYTHONPATH environment variable to include that directory. Then you wouldn't have to modify sys.path directly, it would already contain F:\Python\Projects.

So I've been trying to figure this out but can't seem to get it working. I believe I've added F:\Python\Projects to the PYTHONPATH by going to the Environment Variables under Advanced System Settings (I'm using windows) and under Path adding the directory. But when I call up the path using sys.path it doesn't show. I've tried different things but can't get it to show.

So then I decided to just use sys.path.append and with that the directory does appear but if I want to call the file 'US_data' from F:\Python\Projects\Economic_Data with the from function (from US_data) it doesn't work.

Wall


RE: Calling Variables from Other Files in Different Folders - snippsat - Jul-31-2020

(Jul-31-2020, 03:32 PM)illmattic Wrote: I believe I've added F:\Python\Projects to the PYTHONPATH by going to the Environment Variables under Advanced System Settings (I'm using windows) and under Path adding the directory.
To use PYTHONPATH do it under User Variables.
[Image: 9ODFLu.png]
So now i have made same folders just that i use G:\ Drive.
Now it should show in sys.path test,if not resat Pc.
>>> import sys
>>> 
>>> sys.path
['',
 'C:\\Program Files\\PyScripter\\Lib\\rpyc.zip',
 'G:\\Python\\Projects',
 'C:\\Python38\\python38.zip',
 'C:\\Python38\\DLLs',
 'C:\\Python38\\lib',
 'C:\\Python38',
 'C:\\Python38\\lib\\site-packages',
 'C:\\Python38\\lib\\site-packages\\win32',
 'C:\\Python38\\lib\\site-packages\\win32\\lib',
 'C:\\Python38\\lib\\site-packages\\Pythonwin']
G:\Python\
  |-- __init__.py
  |-- Projects\
    |-- __init__.py 
    |-- Economic_Data\
      |-- __init__.py 
      |-- bar.py
# bar.py
def bar():
    return 42
import for this would be.
>>> import Economic_Data.US_data
>>> 
>>> Economic_Data.US_data.bar()
42
>>> 
>>> # Or
>>> from Economic_Data.US_data import bar
>>> bar()
42



RE: Calling Variables from Other Files in Different Folders - illmattic - Jul-31-2020

I made a mistake. I mistaken path for PYTHONPATH. I actually don't have PYTHONPATH

[attachment=944]


RE: Calling Variables from Other Files in Different Folders - ndc85430 - Jul-31-2020

As an aside, do you still need the empty __init__.py for Python 3? I thought it was only needed for 2.