I am running a jupyter-notebook file (file.ipynb) and trying to import a module "eval_numerical_gradient" from python file "gradient_check" in folder "utils". However, the following code does not work.
from utils.gradient_check import eval_numerical_gradient
Then I try this code, which works:
import sys
sys.path.append("/Users/W/dlp/src/03/utils")
import gradient_check
from gradient_check import eval_numerical_gradient
My question is what is the difference of the two ways above, and is it possible to let the first code work out?
just because you have it under folder utils does not make utils a package. You need an __init__.py file under folder utils if you want to define it as a module.
__init__.py: (place this under utils folder)
from .gradient_check import eval_numerical_gradient
file.ipynb:
import sys
sys.path.append("/Users/w/dlp/src/03")
from utils import eval_numerical_gradient
Related
I have imported a repo using git and when I try to run a file, it shows that "it could not be resolved".
The running script has:
import craft_utils
import test
import imgproc
import file_utils
The imported things are all script.
I have attached the screenshots for the error and hierarchy.
How can I solve this? Thanks.
try adding an empty file named __init__.py in the root folder CRAFT-pytorch (instead of the basenet folder)
create a empty file named __init__.py
it would solve the issue
Add a file inside of the folder called __init__.py
First you need to create an empty file named __init__.py in the folder CRAFT-pytorch
Second add these line at the top of the file you're trying to running, before the imports:
import os
import sys
import inspect
app_path = inspect.getfile(inspect.currentframe())
main_dir = os.path.realpath(os.path.dirname(app_path))
sys.path.insert(0, main_dir)
import craft_utils
import test
import file_utils
I get an import error "unable to import module" when I try to import a module
File structure
src
modules
__init__.py
authenticate_developer
twttier_importer
notebooks
main.ipynb
unit_tests
test_authenticate_developer
In test_authenticate_developer
import requests
from nose.tools import assert_true
import os
import sys
sys.path.append(os.path.abspath('../modules'))
import settings
import twitter_importer #returns import error
import authenticate_developer #returns import error
However when I use the same syntax in my notebooks it is successful.
import sys
import os
sys.path.append(os.path.abspath('../modules'))
sys.path.append(os.path.abspath('../'))
import twitter_importer
import authenticate_developer
import settings
I have looked at existing answers and I tried them out i.e., removing init.py from the root folder and removing or adding init.py to the tests folder. None seems to work for me.
I managed to get someone to help me with these and this is how we tackled it.
src
modules
__init__.py
authenticate_developer
twttier_importer
notebooks
main.ipynb
unit_tests
__init__.py
test_authenticate_developer
In test_authenticate_developer
import os,sys,inspect
sys.path.append(os.path.abspath('../modules'))
#This is to access the parent folder in order to import config
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
import config
from modules.authenticate_developer import Authenticate
I cannot explain why I have to do it differently in the test files but this is my work around
So my project structure is the following:
project/
src/
__init__.py
utils.py
model.py
usage.py
I now want to import functions from utils.py and a class in model.py into my usage.py. But the model.py itself imports functions from utils.py.
So I am doing the following:
# usage.py
from src.model import Model
from src.utils import onehot_to_string
But I am getting the error that it couldnt import functions from utils.py into the model.py:
File "usage.py", line 11, in <module>
from src.model import *
File "/project/src/model.py", line 7, in <module>
from utils import onehot_to_string
ImportError: cannot import name 'onehot_to_string' from 'utils' (/lib/python3.7/site-packages/utils/__init__.py)
I think I am lacking of some basic Python packaging knowledge here. Can someone help me out? :)
Looks like python can't find your utils file in model.py. Then it proceeds to search for utils in path and finds it because, for example, someone has installed some library named utils. Then, the error occurs because this previously installed utils library has no onehot_to_string function.
Try to change your from utils import onehot_to_string to from .utils import onehot_to_string in model.py to use relative import.
for file/function/variables importing use this sys method
import sys
# insert at 1, 0 is for other usage
sys.path.insert(1, '/path/to/application/app/folder')
I have the same problem as expressed here (none of the solutions worked for me). I'll rephrase and tell you exactly where I am.
My folder structure is:
Mar29
utils.py
Subfolder
running.py
Currently, I'm in the directory Mar29/Subfolder, and in the file running.py, I want to import all functions from utils.py.
Here's what I've tried at the top of running.py:
from .. import utils
from . import utils
from ../ import utils
import utils
from utils import *
The most common error I'm getting is:
ValueError: Attempted relative import in non-package
How can I import all functions from the utils.py file?
EDIT: also tried
from ..utils import *
from ../utils.py import *
from ../utils import *
Often got an invalid syntax error.
The parent directory of the package is not being included in sys.path for security reasons. So add it to the path manually:
import sys
sys.path.insert(0,'..')
import utils
I have the following structure of my project
Project\
Core\
__init__.py (empty)
indicators.py
platform_core.py (which imports indicators)
Tests\
test.py (import Core here)
in test.py I have
import sys
sys.path.append(sys.path[0] + "/..")
import Core.platform_core
if I import Core.platform_core and run test.py, I get the following error
ModuleNotFoundError: No module named 'indicators'
This doesn't happen if I import Core. I also tried
import Core.indicators
import Core.platform_core
but get the same error.
My question is whether it is possible to do a relative import of a file that imports another file within the same sub module, without import the whole sub module?
Edit:
In platform_corer.py, the code that imports indicators is
from indicators import SMA