Python access to Parent Directory - python

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 *

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

how to call and execute file in another folders file?

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

How to import a file from a subfolder when you are in another subfolder? (python)

I'm currently in the 'strategy.py' file and I'm trying to import 'utils.py' and 'BaseStrategy.py' ('utils.py' is a file with some functions in it, 'BaseStrategy.py' contains a class with the name 'BaseStrategy').
Folder structure:
program\
__init__.py
main.py
folder1\
__init__.py
utils.py
BaseStrategy.py
folder2\
__init__.py
strategy.py
In 'main.py' I'm able to import both files like this:
from folder1.BaseStrategy import BaseStrategy
from folder1 import utils
So how can I import 'utils.py' and 'BaseStrategy.py' in 'strategy.py'?
import sys
sys.path.insert(0, 'program/folder1')
from folder1 import utils
from folder1 import BaseStrategy
You can also change the:
sys.path.insert(0, 'program/folder1')
To:
sys.path.append('../')
But it will mess up with the import from your parent directory (main.py). However you can overcome that with :
import os,sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
Essentially, you need to put a blank file named __init__.py in the subdirectory. If you want to take a look at the documentation you can do so here (6.4).
find:
./main/
./main/folder2
./main/folder2/__init__.py
./main/folder1
./main/folder1/base.py
./main/folder1/__init__.py
./main/__init__.py
cat ./main/folder1/base.py
import sys
import os
sys.path.append(os.getcwd() + '/..')
import folder2

Python Importing files from other folders (within the same project) using __init__.py?

Trying to import aFile.py from within the bSubFile.py but getting an error saying 'exceptions.ValueError, Attempted relative import in non-package'
My file structure is as follows:
app/
- __init__.py
FolderA/
- __init__.py
- aFile.py
FolderB/
- __init__.py
- bFile.py
SubfolderB/
- __init__.py
- bSubFile.py
I am trying to import aFile from bSubFile.py
tried:
from ..FolderA import aFile
class bSubFile():
...
and:
from ...FolderA import aFile
class bSubFile():
...
but I always get 'Attempted relative import in non-package', I must be missing something very obvious. Thanks!
You can add the other path to your system path. This may be not the most elegant way, but it works.
import sys
import os
# get the folder of the current file, go one directory back and add "FolderA"
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "FolderA"))
# now you can import everything that is in FolderA directly
import aFile

Categories