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
Related
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.
Background
I'm building an importer to encrypt my code. When compiling, I encrypt the code object in pyc files. When loading, I use this customized importer to decrypt the code object before execution.
Since my code is bundled in zip format, I decided to modify zipimporter from the builtin zipimport package by adding the decrypt logic into it.
I'm using Python 3.7
Problem
I modified the zimpimport.c and make it into C extension. The encrypt & decrypt process works fine but I started to see ImportError & AttributeError. For example:
"""module: test.py"""
class Foo:
def bar():
pass
foo = Foo()
// Import Error: can not import name 'foo' from test
// Attribute Error: foo has no attribute bar
When server starts, this error only occurs during the first few minutes randomly. So I suspect that it's a multithreading problem and it's caused by a thread seeing a partially initialized module object.
What I tried
After checking the origin c code, the implementation for loading a module is:
mod = PyImport_AddModuleObject(fullname);
// ... some code
mod = PyImport_ExecCodeModuleObject(fullname, code, modpath, NULL);
It first add module to sys.modules, then execute some c code and execute the module code at last. If another thread read the sys.modules before it finishes executing module code, it can get the ImportError and AttributeError.
I just copy the original implementation and it doesn't use import lock. I guess it's handled by interpreter on outer level and I have to explicitly use import lock. So I wrap the code block with the import lock as following:
#include "import.h"
_PyImport_AcquireLock();
// wrapped code block
_PyImport_ReleaseLock();
My Question
After adding import lock, the error still happens. I'm not familiar with python c api. Have I used the lock in a correct way? Are there other possible reasons that can cause this error?
Code
To test, put the following files in same folder:
zzimporter.c lock added in func zipimport_zipimporter_load_module_impl
zzimporter.h
setup.py
Run python setup.py build_ext --inplace and it will generate zzimporter.so. And it works just like the buit-in zipimport module.
I have two modules in the same directory, one has the function in it. I am trying to call the function into my another module, but I am facing AttributeError
module1:
from tank import cal as c
def water():
lev1=c.rec1
lev2=c.rec2
lev3=c.rec3
print(lev1)
print(lev2)
print(lev3)
module2:
from tank import level as lv
a=input("enter the number")
rec1=a[1:5]
rec2=a[5:9]
rec3=a[9:13]
lv.water()
Error:
AttributeError: module 'tank.level' has no attribute 'water'
Directory Structure:
Data
--tank
--__init__.py
--cal.py
--level.py
You have two modules that are importing each other! You shouldn't have cyclical imports like this; one way to fix this is to have the water() function accept some arguments instead of directly trying to import values from the other module.
def water(lev1, lev2, lev3):
print(lev1)
print(lev2)
print(lev3)
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.....
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.