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.
Related
I ran into some trouble with modules lately. I'm having a hard timefiguring out how I can get the file that has imported the module. basically:
import sys
imported = sys.get_modules_that_have_imported_this_script()
#and then the rest of the code
to be more specific. modules run when you import them right? so is there a way to get what program the module is running from? (preferably the path or file name)
edit: i want the file name so I can use the inspect module to get source code
edit2: I don't want to get the source of the module. I want to find the main program from a module imported within it
edit3: maybe this will help
https://pasteboard.co/OMO6tQoTKALI.png
I found the answer in here How to get filename of the __main__ module in Python?
If you have two files main.py and module.py, this is how you can find the path of main.py from module.py
main.py
import module
module.function()
module.py
import __main__
main_filename = __main__.__file__
def function():
...
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 have two modules in the same directory:
PDSC2.py and db_layer.py
I want to import a class named DBLayer from db_layer.py so I write:
from db_layer.py import DBLayer
But I get an error:
ModuleNotFoundError: No module named 'db_layer'
Does somebdy have an idea what i'm doing wrong?
first of all assume that this python files in the same directory and then remove extension from your code.
from db_layer import DBLayer
or:
from db_layer import *
Is the directory in a place where python searches for modules, python path? Do you have a __init__.py in the directory (it can be blank)?
You need to paste the program file db_layer.py in the \Python\Python36-32\Scripts directory
, then use from db_layer import DBLayer or from db_layer.py import DBLayer to call the desired class in the python program .
Actually sometimes changing the directory of the called module to \Python\Python36-32\Scripts solves these types of problems easily.
This is the solution that worked for me:
import sys
sys.path.append("C:\\Users\\carmel.han\\AppData\\Roaming\\QGIS\\QGIS3\\profiles\\default\\python\\plugins/filterparcel")
from db_layer import DBLayer
I am not a proficient Python coder, hence this might be a basic question:
In my main python code, I load a python code file using (dynamically)
import imp
model = imp.load_source('name','c:/modeldir/modelfile.py')
modelfile.py does an import on the top:
from MyLib import MyLib
MyLib.py is in the same folder as modelfile.py
I get:
ImportError: No module named 'MyLib'
I have also tried:
import os
os.chdir('c:/modeldir')
just before the imp.load_source, did not help.
EDIT:
I am using Python 3.5.2
I've added an empty __init__.py file in 'c:/modeldir'
How to solve this?
The following made it work
sys.path.append('c:/modeldir')
This adds the folder to the python import path and then MyLib can be found
I am creating a python module. To test it i put the file in the same directory and then wrote the code
import mymodule
mymodule.dofunction
python then said >>>no module named mymodule but they are in the same directory.
Adapting from previous answer here. Explicitly state that you want to use the current directory.
Also, consider that you need an "__init__.py" file in each directory you are importing from.
import os, sys
lib_path = os.path.abspath('.')
sys.path.append(lib_path)
import mymodule
More information on the import system here.