Import all functions from a python file one layer up - 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

Related

I need help importing modules into tests from sibling folder

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

Cannot import utils.programs but import utils successfully - python3 import error

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.

Can't import file from another directory which imports a file from the same directory (int Python)

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')

Relative import of a file that imports another file within same submodule

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

Import module from file does not work

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

Categories