a.py
def test():
print("hi")
b.py
from a import test
test('hello')
Error:
ImportError: cannot import name 'test' from 'a' (C:\mypath\a.py)
OR b.py
import a
a.test('hello')
Error:
AttributeError: module 'a' has no attribute 'test'
Thanks in advance!
Edit: I tried to run the script in another directory on my pc so it isnt because the path or something. also, os.getcwd() doesnt work
a.py and b.py must be in the same project forlder.
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
Using python I launch a script from a certain point. From here, there's the subdirectory "A" where it's contained the file "B.py", which contains a class called "C"
from A import C
Traceback
ImportError: cannot import name 'C' from 'A' (unknown location). Is there an easy way to make python look in A then in B and finally get C? Thank you
If I have a directory structure like the following:
A -----------|
|- B.py
test.py
B.py contains the following code:
class C:
def __init__(self):
print("I made it to C!")
If I'm writing test.py to need an instance of C I need to import like this:
from A.B import C
myInst = C()
And I get the following output when I run test.py
I made it to C!
Alternatively, you can add the path to the directory containing B.py to your PATH with the sys module. To show this, I have put the file E.py in the structure of A/B/C/D/E.py along with an empty file named __init__.py
'=
E.py contains the following code:
class F:
def __init__(self):
print("I made it to C!")
test.py contains the following code:
import sys
sys.path.insert(0, "./A/B/C/D/") # You can put the absolute path
from E import F
myInst = F()
And I get the output:
I made it to C!
I have a folder structure similar to this (my example has all the necessary bits):
web-scraper/
scraper.py
modules/
__init__.py
config.py
website_one_scraper.py
Where config.py just stores some global variables. It looks a bit like:
global var1
var1 = "This is a test!"
Within website_one_scraper.py it looks like this:
import config
def test_function():
# Do some web stuff...
return = len(config.var1)
if __name__ == "__main__":
print(test_function)
And scraper.py looks like this:
from module import website_one_scraper
print(website_one_scraper.test_function())
website_scraper_one.py works fine when run by itself, and thus the code under if __name__ == "__main__" is run. However, when I run scraper.py, I get the error:
ModuleNotFoundError: No module named 'config'
And this is the full error and traceback (albeit with different names, as I've changed some names for the example above):
Traceback (most recent call last):
File "c:\Users\User\Documents\Programming\Work\intrack-web-scraper\satellite_scraper.py", line 3, in
<module>
from modules import planet4589
File "c:\Users\User\Documents\Programming\Work\intrack-web-scraper\modules\planet4589.py", line 5, in
<module>
import config
ModuleNotFoundError: No module named 'config'
Also note that In scraper.py I've tried replacing from modules import website_one_scraper with import website_one_scraper, from .modules import website_one_scraper, and from . import website_one_scraper, but they all don't work.
What could the cause of my error be? Could it be something to do with how I'm importing everything?
(I'm using Python 3.9.1)
In your website_scraper_one.py, instead of import config.py try to use from . import config
Explanation:
. is the current package or the current folder
config is the module to import
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 folder with such structure:
parent/
---__init__.py
---SomeClass.py
---Worker.py
First file (__init__.py) is empty.
Second file (SomeClass.py) content is following code:
class Test:
pass
Third file (Worker.py):
import SomeClass
Test()
ImportError: No module named SomeClass
What I do wrong?
Try
from . import SomeClass
but remember you'll have to
SomeClass.Text()
instead of just Test()