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
Related
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
Having trouble importing with a very simple file structure.
My file structure looks like this:
project/
...
project.py
helper.py
__init__.py
...
Within project.py is the class I am trying to import in helper
#project.py
class MyAPIOne():
...
class MyAPITwo():
...
#helper.py
import MyAPIOne
if __name__ == "__main__":
api = MyApiOne()
...
When running with python3 helper.py:
If I keep the absolute import import MyAPIOne I recieve ModuleNotFoundError: No module named 'MyAPIOne'
If I change it to a relative import from . import MyAPIOne I receive ImportError: cannot import name 'MyAPIOne'
I have also experimented with appended to sys.path various directories, with no luck.
If you are running this script from the project folder, you can alter your import the following way: from project import MyAPIOne.
Also, you can add this folder to your PYTHONPATH env variable.
Upd: to add some folder to PYTHONPATH you can ran
export PYTHONPATH="${PYTHONPATH}:/my/other/path"
main file can import all the files, but main file can't be imported by other files.
once file mentioned with __name__ = "__main__", then it becomes main file. so helper.py is acts as main file. it can't be imported.
I am trying to call a script a couple of levels up in a nested folder structure. Each level has a init.py file so that Python recognises the folders as packages. My structure is as so:
Master
__init__.py
script_a.py
----Sub_Folder
----__init__.py
----script_b.py
--------Sub_Sub_Folder
--------__init__.py
--------script_c.py
I am trying to import script_a to script_c using the below statement:
from Master import script_a
...however I am getting the error:
ModuleNotFoundError: No module named 'Master'
What am I doing incorrectly?
Thanks
Imports should be relative to the folder you run your script from. If you are running your script from the Master folder and want to import script_a.py in script_c.py, you should simply do:
import script_a
instead.
for example You are on script_c and and you want to import script_b
Then you can call
from Sub_Folder import script_b
or
from .. import script_b
for calling script_a from script_c use three dots.One dot represents current directory.Two dots represent previous directory and three dots represent previous of previous directory
from ... import script_a
or
import script_a
if you want to import script from current directory you should use
from . import __init__
Anyone knows how to import an entire script from another folder without using os ? specifically I want to import the script utils_database.py in basic_etl.py I tried to use from utils import utils_database in basic_etl.py but it does not work.
Thanks!
The root folder of your project is one level up from the utils folder, so you have to include 01.etl as the top-level package, but then 01.etl is not a valid package name as it starts with a number, so you should rename the folder to something like etl first and then do from etl.utils import utils_database in base_etl.py.
I'm very new to python and I have a directory structure like this:
root
--child
-----config.py
example.py
In example.py I just tried:
import child
but that doesn't seems to be working.
Where I'm making the mistake!
Do you have a __init__.py file in root/child/ directory? After creating this file you should be able to do this:
import child.config
or
from child import config
You can also import multiple modules from child directory like this:
from child import first, second, third
Read about modules and packages here.
If you want to import config.py with importing child you need to define child as a package.
To do so you need to create an __init__.py file in your child directory.
Check this about packages
Your directory should have init.py so that python will understand it is a package. So the directory structure would be like
root
__init__.py
child
__init__.py
config.py
example.py
import root.child
note, you should import root.child not child.
Create an empty __init__.py file in same directory with config.py. This is required for importing files like a package.
Then you can import it.