I am trying out the answer in this.
The __init__.py file in a folder named MyLibs contains:
LogFile = r'D:\temp'
In utils.py in the same folder MyLibs, I tried various ways to access the LogFile variable:
from __init__ import *
print LogFile #Gives: NameError: name 'LogFile' is not defined`:
and:
import __init__
print MyLibs.LogFile #Gives: NameError: name 'MyLibs' is not defined
I got the errors while executing from MyLibs.utils import *
What is the fix I must do? I prefer a method where I can reference LogFile directly without having to add namespace prefixes.
My mistake.
The updated __init__.py was somehow not executed. I started a new Python session and it worked.
Sorry for the false alarm.
Not sure how to do this with 2.7's implicit relative imports, but you could try this:
from __future__ import absolute_import
from . import LogFile
If you're running the Python module directly, you need to run it with python -m MyLibs.utils rather than python MyLibs/utils.py.
Related
In python 2 I can create a module like this:
parent
->module
->__init__.py (init calls 'from file import ClassName')
file.py
->class ClassName(obj)
And this works. In python 3 I can do the same thing from the command interpreter and it works (edit: This worked because I was in the same directory running the interpreter). However if I create __ init __.py and do the same thing like this:
"""__init__.py"""
from file import ClassName
"""file.py"""
class ClassName(object): ...etc etc
I get ImportError: cannot import name 'ClassName', it doesn't see 'file' at all. It will do this as soon as I import the module even though I can import everything by referencing it directly (which I don't want to do as it's completely inconsistent with the rest of our codebase). What gives?
In python 3 all imports are absolute unless a relative path is given to perform the import from. You will either need to use an absolute or relative import.
Absolute import:
from parent.file import ClassName
Relative import:
from . file import ClassName
# look for the module file in same directory as the current module
Try import it this way:
from .file import ClassName
See here more info on "Guido's decision" on imports in python 3 and complete example on how to import in python 3.
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 have 2 scripts.
Main.py
Update.py
I have a function in Main.py which basically does the following:
def log(message):
print(message)
os.system("echo " + message + " >> /logfile.txt")
And in the Update.py file I have a single function which basically does the update. However throughout the update function, it calls "log(message)" with whatever the message is at that point.
The problem now though is I'm getting a NameError: global name "log" is not defined whenever I try use the function outside of the Main.py script.
Any help? on how I would be able to use the function 'log' wherever?
*Code simplified for explanation.
EDIT:
Main.py imports Update from /Scripts/Update.py
Update.py imports log from Main.py
When i try this, it fails saying "cannot import name Update"
Don't import log from Main. That'll rerun the code in Main.py, since running Main.py as a script and importing it as a module aren't equivalent. Other modules should not depend on functions defined in the main script. Instead, put your log function in another module and import that, or have Main.py explicitly pass a logger to the other modules somehow.
Update: You can't import Update because Python can't find it. Python checks in 4 places for modules to import, but the ones you should be interested in are
the directory the script was from, and
the directories specified in the PYTHONPATH environment variable.
You'll either need to put Main.py and the things it imports in the same directory, or add /Scripts to your PYTHONPATH.
Just add in Update.py the line
from Main import log
and you will be able to call log() from Update.py.
You should import the function:
from Main import log
Or best:
import Main
Main.log()
For modules importing each other, refer How can I have modules that mutually import each other.
i'm creating a python program, and i want to split it up into seperate files. I'm using import to do this, but its not working (specifically, a variable is stored in one python file, and its not being read by the main one.
program /
main.py
lib /
__init__.py
config.py
functions.py
I have in main.py:
import lib.config
import lib.functions
print(main)
and config.py has
main = "hello"
I should be getting the output "hello", when i'm executing the file main.py, but i'm not. I have the same problem with functions stored in functions.py
Any help would be great,
Importing the module with a simple import statement does not copy the names from that module into your own global namespace.
Either refer to the main name through attribute access:
print(lib.config.main)
or use the from ... import ... syntax:
from lib.config import main
instead.
You can learn more about how importing works in the Modules section of the Python tutorial.
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.