I have a project structure that looks like:
The file greet.py is given as:
def greet_morning(message):
print("Hello, {}", message)
def greet_evening(message):
print("Evening message: {}", message)
and the file msg.py is given as :
import sys
import os
sys.path.append(os.getcwd())
from greet.greet import greet_morning
greet_morning("heyy")
When I try to run msg.py as python message/msg.py, I get an error saying ImportError: No module named greet.greet. I am running this file from the root. Why do I get this error, when I have already added the cwd in the system path?
I think is
from untitled.greet.greet import greet_morning
if still doesnt work, add:
import sys
sys.path.append('../')
edit
I think you may find all the possible solutions here Importing files from different folder
add __init__.py inside greet and msg folder__init__.py
You have missed the file __init__.py in your module.
Just create an empty file __init__.py in greet folder.
Related
I am running python code in docker.
I have the following file structure:
-my_dir
-test.py
-bird.py
-string_int_label_map_pb2.py
-inference.py
inference.py:
import test
import bird
from string_int_label_map_pb2 import StringIntLabelMap
test.py and bird.py both contain this code:
print('hello world!')
def this_is_test():
return 'hi'
In inference.py 'import test' throws no error(but 'hello world!' is never printed).
'import bird' throws: ModuleNotFoundError: No module named 'bird'. Finally
'import string_int_label_map_pb2 ' throws: ModuleNotFoundError: No module named 'string_int_label_map_pb2 '
Note: bird.py and test.py are not simultaneously existing in the dir, they are depticted as such only to make my point. They exist only when I rename the same file to either name to test if the name itself was to blame.
If one or the other is not in the folder but you still have import test and import bird in the inference.py, that's your issue. Python is trying to import both files but can not find the file.
I added bird.py to /worker/ (the WORKDIR path configured in the dockerfile) and sure enough "Hello world!" printed.
So the issue was that inference.py was not searching its parent dir for imports as I thought, but rather the path configured in WORKDIR.
Still don't understand why import test gave no errors since there was never a test.py file in /worker/.
I also had this problem, how did I solve the problem?
For example.
My directory:
--Application
---hooks
----init.py
----helpers.py
---model
----init.py
----model_person.py
In script model_person.py
import sys
sys.path.append('../')
from hooks.helpers import *
Thats all
I have a module 'hydro' with the structure:
hydro/
__init__.py
read.py
write.py
hydro_main.py
This gets used as a submodule for several other modules, which have scripts with similar names:
scenarios/
__init__.py
read.py
write.py
scenarios_main.py
hydro/
__init__.py
read.py
write.py
hydro_main.py
In order to keep the script names straight, I want to specify the module name on import. So in the header of hydro_main.py, I'd have:
import hydro.read
and in scenarios_main.py, I'd have:
import hydro.read as read_hydro
import scenarios.read as read_scenarios
The problem is that when I attempt to run hydro_main.py from the package root, I get the following error:
ModuleNotFoundError: No module named 'hydro'
How can I set the package name for 'hydro' such that it will allow me to refer to the package name on import? I thought adding __init__.py was supposed to initialize the package, but __package__ still comes back as None.
You can import just the entire module as one instance.
import hydro
from hydro import read as read_hydro, hydro_main as main
hydro.hydro_main()
main() # same as above
hydro.read()
read_hydro() #same as above
It is a sub module so you have to use parentModule.subModule.* . Your first line will change to import scenarios.hydro.read as read_hydro
scenarios/hydro/hydro_main.py
print("I am in hydro_main")
scenarios/hydro/read.py
print("I am in hydro read")
scenarios/hydro/write.py
print("I am in hydro write")
scenarios/read.py
print("I am in scenarios read")
scenarios/write.py
print("I am in scenarios write")
scenarios/scenarios_main.py
import scenarios.hydro.read as read_hydro
import scenarios.read as read_scenarios
I am in hydro read
I am in scenarios read
I am getting an error ImportError: cannot import name 'dataset_builder'. The import command is following:
from object_detection.builders import dataset_builder
The file tree looks following:
-ObjectDetection
-train.py
-models
-research
-object_detection
-builders
- __init__.py
-dataset_builder.py
I am running the train.py from the root directory (os.getcwd() returns following path C:\Users\horakm\PyCharmProjects\ObjectDetection) and I added in the train.py following code to add paths:
sys.path.append(r'C:\Users\horakm\PyCharmProjects\ObjectDetection\models')
sys.path.append(r'C:\Users\horakm\PyCharmProjects\ObjectDetection\models\research')
sys.path.append(r'C:\Users\horakm\PyCharmProjects\ObjectDetection\models\research\slim')
When I print all paths using sys.path i get this:
['C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\python36.zip',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\DLLs',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages\\win32',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages\\win32\\lib',
'C:\\Users\\horakm\\AppData\\Roaming\\Python\\Python36\\site-packages\\Pythonwin',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-packages',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-
packages\\object_detection-0.1-py3.6.egg',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-packages\\win32',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-
packages\\win32\\lib',
'C:\\Users\\horakm\\AppData\\Local\\Continuum\\miniconda3\\envs\\tf1_env\\lib\\site-packages\\Pythonwin',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection\\models',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection\\models\\research',
'C:\\Users\\horakm\\PyCharmProjects\\ObjectDetection\\models\\research\\slim']
How is it possible that the import statement doesn't work?
Which object are you actually trying to import?
All that are defined in dataset_builder? In that case it should be from object_detection.builders.dataset_builder import *.
Or is the object also called dataset_builder? In that case it is from object_detection.builders.dataset_builder import dataset_builder.
I'm new to python and I'm having this problem that I can't figure it out.
My file structure is:
enter image description here
On Criador.py I have several functions, for example:
def doSomething():
pass
def doSomethingElse():
pass
and Im trying to use one of this functions on the Controller.py file:
The first thing I did was, on the Controller.py:
import Controller.Criador
and then tried to use that function as:
Controller.Criador.doSomething()
After running Controller.py, I got this error:
ModuleNotFoundError: No module named 'Controller.Criador'; 'Controller' is not a package
I tried several other things, like:
from . import Criador
or
from Controller.Criador import doSomething
or
from Controller import Criador
and nothing helped, just changed the errors to:
ImportError: cannot import name 'Criador'
and
ModuleNotFoundError: No module named 'Controller.Criador'; 'Controller' is not a package
and
ImportError: cannot import name 'Criador'
Can someone give me a light about this? I'm using PyCharm and it does not give me any error when I declare the imports, only when I run the file
If Controller.py and Criador.py are in the same folder, you can do this inside Controller.py:
import Criador
Criador.doSomething()
I have a problem importing a module:
It is under this directory ./dao and the code that calls it is here ./core. Schematically represented as:
rnaspace/
__init__.py
core/
__init__.py
logger.py
dao/
__init__.py
storage_configuration_reader.py
This is the error message:
Traceback (most recent call last): File "logger.py", line 21, in <module>
from rnaspace.dao.storage_configuration_reader import storage_configuration_reader ImportError: No module named rnaspace.dao.storage_configuration_reader
This file it is there /rnaspace/dao/storage_configuration_reader.py and in the same folder the __init__.py file as follows:
""" Package dao
Gathers files that access to the plateform data
"""
If I understood well this question, it should work. I think that the problem is that one is not the subdirectory of the other (or that the path is not exaclly that one), there is a way to go around it? Or need I to apply the solution to this question?
EDIT
The __init__.py file of the rnaspace folder:
import rnaspace.dao.storage_configuration_reader as scr
def update_conf(conf_path, predictors_conf_dir):
scr.update_conf(conf_path, predictors_conf_dir)
from rnaspace.dao.storage_configuration_reader import storage_configuration_reader
That is wrong because there is no "storage_configuration_reader" directory in "dao" directory
This is how it should be:
from rnaspace.dao import storage_configuration_reader
EDIT:
or this way:
import rnaspace.dao.storage_configuration_reader
I finally found the solution in another question, it is using the module imp.
I just needed to add the name of the module, and the absolute path where it was:
imp.load_source("storage_configuration_reader","./rnaspace/dao/storage_configuration_reader.py")