I got an error,ModuleNotFoundError: No module named 'test2'.I made test1.py and test2.py.I wanna call test2.py's module in test1.py.So I wrote codes import test2 as test in test1.py but the error happens.I rewrote from test2 import test but same error happens.What is wrong in my code?How should I fix this?I am using IDE so I think I can install module automatically, but in this time it cannot be done.
Your syntax is wrong....
from test2 import test
means from the test2 module import a function or variable named test...
The actual syntax is import test2...In that case make sure that the two python files are in the same directory.....
Related
So i'm using a RPI4 to program some code in python. As it was growing i decided to separate this in multiple scripts/modules according to functions, variables and classes.
After doing all that i got an error when calling the function after import. I tried adding init.py, but it shouldn'0t be necesary as the modules are in the same folder.
I tried with some tests scripts/modules and got the same error:
Folder: test
--test
----test1.py
----test2.py
test2.py:
def print_test():
print("test")
for test1.py i have 2 cases:
case1, test1.py:
import test2
test2.print_test()
error: AttributeError: module 'test2' has no attribute 'print_test'
case2, test1.py:
from test2 import *
print_test()
error: NameError: name 'print_test' is not defined
i've lost a lot of time on this and after extensive searching, nothing seems to work
I have a Jupyter Notebook - we'll call it main, and I'm running it piecemeal in Jupyter Lab 3.0.14 using a Python 3 environment.
Within this notebook, I'm trying to import custom functions from a module, we'll call this one module.py, which is in the same directory as main.
It looks something like this:
//main
import sys
import os
from module import foo, bar
//module.py
def foo():
return
def bar():
return
When I run the import statements in main, it throws an error
ImportError: cannot import name 'bar' from 'module' (/module.py)
If I run
from module import foo
or
from module import *
It's fine (although calling the functions will throw an error that the function is not defined). In fact, it will import foo even if there's no function named foo in module.py. It won't import any of my own functions by name. I feel like I must be missing something fundamental, here...
Any help would be greatly appreciated!!
Okay, this is my first time using Jupyter Lab, and this problem came from a fundamental misunderstanding of how Jupyter (and maybe other IDEs?) manages packages.
In short, as I understand it, re-running the code which imported functions from module.py did not actually re-import the contents. The only things it imported were what I asked for the first time, and any subsequent calls to import functions only referenced that first static image I pulled.
Therefore, if module.py had one function, foo, and I ran:
from module import foo
...then made some changes, such as editing the contents of foo and adding bar, running:
from module import foo, bar
...didn't actually do anything but reference that first copy of module.py I pulled. Because bar didn't exist the first time I ran the import function, it "couldn't see it", no matter how many times I re-ran the import function. I had to restart the kernel to fix it, basically.
I noticed this because when I changed the name of the script, it would import the functions that weren't previously working, but then edits to the functions, even once imported, weren't recognized.
This link provided the clue I needed to figure this out.
Hopefully this helps somebody!
Restart the kernel in the Jupiter notebook. I solved a similar import name issue by restarting the kernel.
I have a github repo named pandora:
https://github.com/akshkr/pandora-python
In the test_case_refactor branch, I can't use submodules of pandora such as pn.util.misc.seed_everything() gives me the following error.
AttributeError: module 'pandora' has no attribute 'util'
I doubted circular imports so moved every import inside function wherever possible but didn't seem to work. And the test-cases written inside the module are passing.
Can someone tell me what exact issue is causing this AttributeError?
Since misc is a python script and not a module in itself, you need to either import the function itself
from pandora.util.misc import seed_everything
seed_everything()
OR, you can import the misc script from util module
from pandora.util import misc
misc.seed_everything()
I am newbie to Python 3 and currently learning how to create Python modules. I have created below package structure.
maindir
test.py
package
__init__.py
subpackage
__init__.py
module.py
This is my module.py file
name="John"
age=21
and this is my test.py file
import package.subpackage.module
print(module.name)
When I run test.py I am getting this error NameError: name 'module' is not defined
However, when I change the import statement to import package.subpackage.module as mymod and print the name with print(mymod.name) then its working as expected. Its printing name John.
I didn't understand why its working with second case and not with first.
Perhaps what you were attempting was this:
from package.subpackage import module
Then you can reference module as a name afterwards.
If you do:
import package.subpackage.module
Then your module will be called exactly package.subpackage.module.
With little bit of reading I understood this behaviour now. Please correct me If I am wrong.
With this import package.subpackage.module style of import statement you have to access the objects with their fully qualified names. For example, in this case
print(package.subpackage.module.name)
With aliasing I can shorten the long name with import package.subpackage.module as mymod and print directly with print(mymod.name)
In short print(package.subpackage.module.name) == print(mymod.name)
I'm Python newbie, and I have a problem with an error message
'ImportError: cannot import name callfunc'
I made two python files, '~/a.py' and '~/pkg/b.py'. (Additionally my IDE automatically created '~/__init__.py' and '~/pkg/__init__.py')
in a.py, a function
def callfunc():
print "Called"
is defined, and there are two statements in pkg/b.py:
from a import callfunc
callfunc()
However when executing python pkg/b.py, an error raises :
ImportError: cannot import name callfunc
I tried export PYTHONPATH=... , but it isn't effective also
How can I solve this problem?
write this in b.py before any of the imports from your own modules:-
import sys
sys.path.append(<the directory where a.py is defined>)
what the value of PYTHONPATH?
The parameter can be $HOME in your example.