how to call and execute file in another folders file? - python

I need to call 'test.txt' file in 'req.py' to execute some data.
File structure:
application - 1.Foldername -- req1.py
2.test.txt
will the same structured will be followed in the azure function app to call text.txt?
#req1.py
file1 = open(r'application/test.txt',"r")
print(file1.readlines())
file1.close()

Yes we can call the files from other folders and can read them, as below:
ReproScreenshot
We can just simply import .py files in the same folder by adding import pythonfile
Below are few ways like how we can include python files.
Including modules:
import sys
# Insert the path of modules folder
sys.path.insert(0, "C:\\Users\\anila\\Desktop\\Task\\modules")
# Import the module0 directly since
# the current path is of modules.
import firstModule
# Prints "Module0 imported successfully"
firstModule.run()
Import method:
import sys
sys.path.insert(0, 'path/to/your/py_file')
import py_file

Related

Can't import module from different directory python

So below I have is my file structure:
/
api.py
app.py
code/
data_api/
weather_data/
get_data_dir/
get_data.py
master_call_dir/
master_call.py
My current problem is that I'm importing master_call.py from inside app.py I do this with the following code:
-- app.py
import sys
sys.path.append('./code/data_api/weather_data/master_call_dir')
from master_call import master_call_interface as master_call
Importing master_call itself works perfectly fine, the issue is that inside off master_call I import get_data.py. I do this with the following code:
-- master_call.py
import sys
sys.path.append("../get_data_dir")
from get_data import get_data_module
When printing out sys.path I can see ../get_data_dir inside of it but Python still isn't able to find the get_data.py file in /get_data_dir.
Does anyone know how to fix this?
(there is an __init__.py file in every directory, I removed it here for readability)
So I figured it out. The problem is while the directory of the master_call.py file is /code/data_api/weather_data/master_call the current working directory (CWD) is / since that is where app.py is located.
To fix it we simply fetch absolute file path in each python script with
current_path = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
and pre-pend it to any file path we're appending with sys.path.append
So it'd look like this in practice:
import sys
import os
current_path = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
sys.path.append(f'{current_path}./code/data_api/weather_data/master_call_dir')
from master_call import master_call_interface as master_call

How can I import module from another folder which is not same directory structure

I have two directory on same level
1.Scripts->(Inside it we have app.py file)
2. Keymaker-->(Inside it we have keymaker.py file)
Now I want to import keymaker.py in app.py, So how Can I do it
Write these lines in app.py file.
import sys
sys.path.append('path/to/Keymaker/dir')
After that you can import anything from keymaker.py file using
from keymaker import *
Add empty __init__.py file to Keymaker directory and do from Keymaker import keymaker

Have problems in importing classes from another file in parent folder

The files I need in the directory look like
face/util/load_data.py
face/preprocessing.py
preprocessing.py defines the classes FaceDetector, FaceAligner and the method clip_to_range
I want to import these classes into load_data.py
I am trying to execute this statement inside load_data.py
from preprocessing import FaceDetector, FaceAligner, clip_to_range
I am getting the error
Traceback (most recent call last):
File "utils/load_data.py", line 7, in <module>
from preprocessing import FaceDetector, FaceAligner, clip_to_range
ImportError: cannot import name 'FaceDetector'
Can you please tell me how to correctly import these classes?
You can try adding __init__.py
Which will allow you to import face module, then you can import like,
from face.preprocessing import FaceDetector, FaceAligner, clip_to_range
Update:
Another way is to insert module in sys.path,
import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
import preprocessing
move preprocessing.py to util directory.
Look for the file in the path of the executed file.
If you do not want to change the directory structure. Add the following code.
import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
Import is possible regardless of directory structure.
You will can solved.

Python access to Parent Directory

I have a file in the directory
app
a
Ulil.py
b
main.py
I want to import Ulil.py (at app\a) into main.py (at app\b).
How do i go about doing this. I need to move the file around as well so I don't want to put the entire path. I just to be in app\b and access only app\a. Folder name stays the same.
You can add the directory to sys.path:
import sys
sys.path.insert( 0, '../a' )
import Util
First, create an empty file called __init__.py in a
Second, in main.py in b import it using
import sys
sys.path.insert( 0, '../' )
from a.Util import *

Python import file with dependencies in the same folder

I have a file ref.py that depends on a text file ex.txt, that is in the same directory \home\ref_dir . So it works normally when I run the file ref.py, but if I try to import ref.py to another file work.py in a different directory \home\work_dir , I do the following
import sys
sys.path.append('\home\ref_dir')
import ref
But then I get an error, that the program cannot find ex.txt
How can I solve this issue without using absolute paths. Any ideas?
Use the os module to get access to the current absolute path of the module that you're in, and then get the dirname from that
You would want to open ex.txt in your file like this.
import os
with open('%s/ex.txt' % os.path.dirname(os.path.abspath(__file__)) as ex:
print ex.read()

Categories