I have the following the directory structure
pytest_testing/
__init__.py
math_ops.py
dbs/
__init__.py
dbConnect.py
tests/
test1.py
Now in test1.py i am trying to do import a function from dbConnect module, like this from pytest_testing.dbs.dbConnect import query_data but i get error "No Module named pytest_testin.
If i try the same in a directory above pytest_testing everything works just fine. Am i missing something here ?
Well, you can just type this from dbs.dbConnect import query_data.
I think this can work well
If you are using Linux or Windows use the code:
# test1.py
import sys
sys.path.append(/path/to/pytest_testing/dbs/)
from dbConnect import functionName #dbConnect without .py extension
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 have 3 python file :
test.py
modules(folder) and in modules there 3 to files : module1.py module2.py init.py
test.py
./test.py
./modules
./modules/module1.py
./modules/module2.py
./modules/__init__.py
module1.py:
from module2 import temp
def print_temp():
print(temp)
if __name__=='__main__':
print_temp()
module2.py
temp =1
test.py
from modules.module1 import print_temp
print_temp()
When I run python test.py I got ModuleNotFoundError: No module named 'module2'
How can I fix that please?
When you import a module in Python, it searches the PYTHONPATH for a module / package with that name. But it doesn't search inside directories so you have to specify.
If you want to be able to run modules.module1 directly (as well as import it) then you must use the full path:
from modules.module2 import temp
But if you just want to be able to import the module and not run it directly it is better to use a relative import as it will still work even if the name of the package is changed:
from .module2 import temp
The following is my folder structure:
/experiments
/experiment_1
/experiment_2
/experiment_3
/src
/sample.py
helper.py
All experiments share some code, which I have exported into helper.py. Now I'm trying to import a function from helper.py into sample.py:
from ...helper import my_function
and I get the following error:
ImportError: attempted relative import with no known parent package
Please note that I have tried many solutions offered here, but none worked.
create a __init__.py file in your parent folder.
The above question is related to
Importing files from different folder
The solution is to add the following lines in the sample.py:
import sys
sys.path.insert(1, 'path-to-experiments')
from helper import get_json
I've a python file named sample.py which has some functions and another python file from which I want to access the functions of the sample.py. I've tried the code below which is working fine if I include the directory in the import statement.
from Integrated.sample import *
But the folder can't be the same for my application. Also, I referred another problem in stackoverflow similar to my issue, tried one of the answers
from .sample import *
which gives the following error
ModuleNotFoundError: No module named '__main__.crypto'; '__main__' is not a package
directory structure:
-module
--__init__.py
--sample.py
--tester.py
Hoping for a solution
Thanks in advance
If they are in the same module, you can directly import the file as given below. Works for me.
sample.py
def myfun():
print("Sample")
tester.py
from sample import myfun
myfun()
Output
$ python3 tester.py
Sample
If I understand you question correctly, you would like to import a python module from another python folder.
I see you already have a __init__.py file that is needed to declare this folder is a python package.
Navigate to the file where you want to import the module.
from root.parent.folder.file import function_name
other way of doing it
import sys
import os
sys.path.append(os.path.abspath("/home/helloworld"))
from helloworld import *
Hope this helps.
My apps are organized like this:
apps/
code/
libglobal/
funglobal.py
tests/
project/
liblocal/
funlocal.py
main.py
In main.py I have:
import liblocal.funlocal
In funlocal.py I try to import funglobal.py with:
from ....code.libglobal import funglobal
When I run
python3 -B tests/project/main.py
I get an error:
from ....code.libglobal import funglobal
ValueError: attempted relative import beyond top-level package
I have read a lot of information about relative imports with python3 and still don't find how to solve this error without changing the apps organization radically. Any solution?
As the script being executed has its __name__ set as __main__ and defines itself to be on the top level of the package, it refuses to recognize scripts in sibling directories.
You can fix this with a sys.path hack:
import sys, os
sys.path.insert(0, os.path.abspath('../..'))
or an interseting alternative with setuptools is presented in this answer.
Have you a __init__.py script in each folder ?
If no, you should create an empty script named __init__.py in each folder.