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
Related
I'm trying to import an class into my test (test_account.py), but I get the following error:
from ..src.account_status import TestClass
ImportError: attempted relative import with no known parent package
This is my directory I have scr and test in the same App directory.
./App/src/account_status.py
./App/test/test_account.py
My import in test_account.py:
from ..src.account_status import TestClass
I have tried adding __init__.py to both folders and the App directory, but I get the same message, why?
Try this
import sys
sys.path.append("../src")
from account_status import TestClass
sys.path.remove("../src")
I am trying to implement the code in https://github.com/kexinyi/ns-vqa.
However, when I try the command, python tools/preprocess_questions.py \ ... in the section of "Getting started". I see a message No module named 'utils.programs'.
Then I install utils and which makes import utils work, but import utils.programs does not work.
Does anyone have any idea to solve it?
import os
import argparse
import json
import h5py
import numpy as np
import utils.programs as program_utils # this one cannot be imported
import utils.preprocess as preprocess_utils
import utils.utils as utils
Solution:
Add the below lines at the beginning of preprocess_questions.py file.
import sys
sys.path.insert(0, "..")
This should solve your problem.
Explanation:
It is failing because preprocess_questions.py don't know about the path of utils.programs to import. With the above lines added to the path using .., the required file will be imported.
For more on this refer how importing works in python.
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 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
Edit: __init__.py files are included, but I'm using Python 3 - I don't think it matters.
Another edit: Anything in config.py will import with no problem. If I simply omit from cache import Cache then no errors. Interestingly, no errors occur when importing Config in config.py
I cannot figure out what's wrong here. I'm getting an error whenever I try to import a specific class. Here's what my project layout looks like:
app/
dir1/
config.py
cache.py
manager.py
__init__.py
test/
test.py
__init__.py
cache.py:
import sys
import os
sys.path.append(os.path.dirname(__file__))
from manager import Manager, AnotherClass
from config import Config
manager.py
import sys
import os
sys.path.append(os.path.dirname(__file__))
from config import Config
from cache import Cache
test.py
cwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.abspath(os.path.join(cwd, os.pardir)) + '/dir1')
from cache import Cache, AnotherClass
from manager import Manager
test = Cache()
...
So when I run test.py I get this:
File "/path/to/project/app/dir1/<module>
from cache import Cache
ImportError: cannot import name 'Cache'
from manager import Manager line 5,
Even though config.Config loads just fine, no errors there, but as soon as I try to import cache.Cache it suddenly can't find or import any class in cache.py. All files have the same permissions. Can someone tell me what's wrong here?
You are missing the __init__.py file in your module
app/
__init__.py
dir1/
__init__.py
config.py
cache.py
manager.py
test/
test.py
and instead of messing with sys.path should do a relative import like
from .config import Config
from .cache import Cache
Python 2 may also need a line
from __future__ import absolute_import
before those imports.