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.
Related
I am brand new to working with python so this question might be basic. I am attempting to import five helper files into a primary script, the directory setup is as follows and both the script I'm calling from and the helper scripts are located within src here in this path-
/Users/myusername/Desktop/abd-datatable/src
I am currently importing as-
import helper1 as fdh
import helper2 as hdh
..
import helper5 as constants
The error I see is
File "/Users/myusername/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/22.77756/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'src'
I have also attempted the following for which the import was still unsuccessful:
import src.helper1 as fdh
..
import src.helper5 as constants
and
from src import src.helper1 as fdh
...
from src import helper5 as constants
and tried adding the following to the head of the script-
import sys
import os
module_path = os.path.abspath(os.getcwd())
if module_path not in sys.path:
sys.path.append(module_path)
Would be very grateful for any pointers on how to debug this!
Seems likely that it's a path issue.
In your case, if you want to import from src, you would need to add Users/myusername/Desktop/abd-datatable to the path.
os.getcwd() is getting the path you invoke the script from, so it would only work if you are invoking the script from Users/myusername/Desktop/abd-datatable.
I have two scripts in the same folder:
5057_Basic_Flow_Acquire.xyzpy
5006_Basic_Flow_Execute.xyzpy
I need to call function run() from 5057_Basic_Flow_Acquire file in the 5006_Basic_Flow_Execute file.
I tried two approaches:
1.
import time
import os
import sys
import types
dir_path = os.path.dirname(os.path.realpath(__file__))
#os.chdir(str(dir_path))
sys.path.append(str(dir_path))
sensor = __import__('5057_Basic_Flow_Acquire')
import time
import os
import sys
import types
import importlib
import importlib.util
dir_path = os.path.dirname(os.path.realpath(__file__))
spec = importlib.util.spec_from_file_location('run', dir_path+'\\5057_Basic_Flow_Acquire')
module = importlib.util.module_from_spec(spec)
Pycharm is reporting this type of errors:
case:
ModuleNotFoundError: No module named '5057_Basic_Flow_Acquire'
case
AttributeError: 'NoneType' object has no attribute 'loader'
So in both cases module was not found.
Does someone have any suggestion?
You should rename your files to something like :
5057_Basic_Flow_Acquire_xyz.py
5006_Basic_Flow_Execute_xyz.py
so that they are recognized as modules and can be imported.
Then, in your 5057_Basic_Flow_Acquire_xyz module, you can import the run function with the following line :
from 5006_Basic_Flow_Execute_xyz import run
Both files are in the same directory, so you should be able to import without changing your PATH environment variable as the current directory is automatically added at the top of the list.
I am trying to import a file from a folder named pytorch_net from a folder named AI_physicist into a script named models.py. I have tried to change the folder locations of the files, get an init.py file into the main AI_physicist folder, and change the sys.path.append command to get only the folder with the files inside of it. I have also looked at other posts and have tried their solutions but to no avail.
Here are the posts that I have found and have tried:
What does os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) mean? python
import python module using sys.path.append
file structure:
C:-----
Users----
trevo----
Desktop----
AI_physicist----
pytorch_net----
theory_learning----
Here is the error in its entirety:
No module named 'pytorch_net'
File "C:\Users\trevo\OneDrive\Desktop\AI_physicist\theory_learning\models.py", line 13, in <module>
from pytorch_net.net import MLP
File "C:\Users\trevo\OneDrive\Desktop\AI_physicist\theory_learning\theory_model.py", line 24, in <module>
from AI_physicist.theory_learning.models import Loss_Fun_Cumu, get_Lagrangian_loss
And lastly here is the code that I am using:
models.py:
# coding: utf-8
# In[ ]:
import numpy as np
import torch
import torch.nn as nn
from torch.autograd import grad
import sys, os
sys.path.append(os.path.join(os.path.dirname("__file__"), '..', '..'))
from AI_physicist.pytorch_net.net import MLP
from AI_physicist.pytorch_net.util import get_criterion, MAELoss, to_np_array, to_Variable, Loss_Fun
from AI_physicist.settings.global_param import PrecisionFloorLoss, Dt
from AI_physicist.theory_learning.util_theory import logplus
Any help on what I would be missing would be very appreciated, thank you!
Try to put __init__.py not init.py in that folder.
Alternatively:
sys.path.append(f'{os.path.dirname(__file__)}\\folder_name')
Please look on the difference between my and yours sys.append.
That should always work. Bare in mind these will work only if your script is in a subfolder of your main folder with main.py in.
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
My script is trying to read my utils from a different folder. I keep getting Import Error. PFB my script :
import sys
import os
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname+"/../../dags/golden/")
dname = os.path.dirname(abspath)
sys.path.append(dname)
import utils
semantics_config = utils.get_config()
And my folder structure is as follows :
/home/scripts/golden/script.py
/home/dags/golden/utils.py
Output is :
Traceback (most recent call last):
File "check.py", line 22, in
import utils
ImportError: No module named utils
Any pointers will be greatly helpful!
Try this
import sys
import os
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname+"/../../dags/golden/")
dname = os.getcwd()
sys.path.append(dname)
import utils
semantics_config = utils.get_config()
You are again assigning "dname" as old path.
So use os.getcwd()
or
import sys
import os
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
sys.path.append(dname+"/../../dags/golden/")
import utils
semantics_config = utils.get_config()
You made a mistake in your script.
os.chdir(dname+"/../../dags/golden/"), only changes your current working directory, but not change the value of variable "abspath"
So the value of "dname" keep the same before and after your "os.chdir"
just do sys.path.append(dname+"/../../dags/golden/"), you will get what you want.
BTW, python is very easy to locating the problem and learn. Maybe, You just need to add a print before you using a variable. And, don't forge to remove those print before you release your scripts.