I am trying to import a module from an external file as follows:
>>> from sys import path
>>> path.append("/tmp/security/")
>>> from util import set_archive
When I run this as root it works fine. But when I run this as testuser it does not work.
permissions on /tmp/security/util.py is as follows:
[root#dashmpp-head-0 - Db2wh /]# ls -l /tmp/security/util.py
-rwx------ 1 db2inst1 root 88046 Dec 4 19:08 /tmp/security/util.py
when I try to import as testuser I get:
>>> from sys import path
>>> path.append("/tmp/security/")
>>> from util import set_archive
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'util'
>>>
testuser belongs to another groups called db2iadm1
Is there any way I can import the module as root so I can use set_archive function?
Related
This question already has answers here:
Use 'import module' or 'from module import'?
(23 answers)
Closed 8 years ago.
I'm wondering if there's any difference between the code fragment
from urllib import request
and the fragment
import urllib.request
or if they are interchangeable. If they are interchangeable, which is the "standard"/"preferred" syntax (if there is one)?
It depends on how you want to access the import when you refer to it.
from urllib import request
# access request directly.
mine = request()
import urllib.request
# used as urllib.request
mine = urllib.request()
You can also alias things yourself when you import for simplicity or to avoid masking built ins:
from os import open as open_
# lets you use os.open without destroying the
# built in open() which returns file handles.
Many people have already explained about import vs from, so I want to try to explain a bit more under the hood, where the actual difference lies.
First of all, let me explain exactly what the basic import statements do.
import X
Imports the module X, and creates a reference to that module in the
current namespace. Then you need to define completed module path to
access a particular attribute or method from inside the module (e.g.:
X.name or X.attribute)
from X import *
Imports the module X, and creates references to all public objects
defined by that module in the current namespace (that is, everything
that doesn’t have a name starting with _) or whatever name
you mentioned.
Or, in other words, after you've run this statement, you can simply
use a plain (unqualified) name to refer to things defined in module X.
But X itself is not defined, so X.name doesn't work. And if name
was already defined, it is replaced by the new version. And if name in X is
changed to point to some other object, your module won’t notice.
This makes all names from the module available in the local namespace.
Now let's see what happens when we do import X.Y:
>>> import sys
>>> import os.path
Check sys.modules with name os and os.path:
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
Check globals() and locals() namespace dict with name os and os.path:
>>> globals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> locals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> globals()['os.path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'os.path'
>>>
From the above example, we found that only os is added to the local and global namespaces.
So, we should be able to use os:
>>> os
<module 'os' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> os.path
<module 'posixpath' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>
…but not path:
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Once you delete the os from locals() namespace, you won't be able to access either os or os.path, even though they do exist in sys.modules:
>>> del locals()['os']
>>> os
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> os.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
Now let's look at from.
from
>>> import sys
>>> from os import path
Check sys.modules with name os and os.path:
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
So sys.modules looks the same as it did when we imported using import name.
Okay. Let's check what it the locals() and globals() namespace dicts look like:
>>> globals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> locals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['os']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'os'
>>>
You can access by using path, but not by os.path:
>>> path
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> os.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
Let's delete 'path' from locals():
>>> del locals()['path']
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
One final example using aliasing:
>>> from os import path as HELL_BOY
>>> locals()['HELL_BOY']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['HELL_BOY']
<module 'posixpath' from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>
And no path defined:
>>> globals()['path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'path'
>>>
One pitfall about using from
When you import the same name from two different modules:
>>> import sys
>>> from os import stat
>>> locals()['stat']
<built-in function stat>
>>>
>>> stat
<built-in function stat>
Import stat from shutil again:
>>>
>>> from shutil import stat
>>> locals()['stat']
<module 'stat' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc'>
>>> stat
<module 'stat' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc'>
>>>
THE LAST IMPORT WILL WIN
There is a difference. In some cases, one of those will work and the other won't. Here is an example: say we have the following structure:
foo.py
mylib\
a.py
b.py
Now, I want to import b.py into a.py. And I want to import a.py to foo. How do I do this? Two statements, in a I write:
import b
In foo.py I write:
import mylib.a
Well, this will generate an ImportError when trying to run foo.py. The interpreter will complain about the import statement in a.py (import b) saying there is no module b. So how can one fix this? In such a situation, changing the import statement in a to import mylib.b
will not work since a and b are both in mylib. The solution here (or at least one solution) is to use absolute import:
from mylib import b
Source: Python: importing a module that imports a module
You are using Python3 were urllib in the package. Both forms are acceptable and no one form of import is preferred over the other. Sometimes when there are multiple package directories involved you may to use the former from x.y.z.a import s
In this particular case with urllib package, the second way import urllib.request and use of urllib.request is how standard library uniformly uses it.
In python 2.x at least you cannot do import urllib2.urlopen
You have to do from urllib2 import urlopen
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2.urlopen
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named urlopen
>>> import urllib.request
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named request
>>>
My main complaint with import urllib.request is that you can still reference urllib.parse even though it isn't imported.
>>> import urllib3.request
>>> urllib3.logging
<module 'logging' from '/usr/lib/python2.7/logging/__init__.pyc'>
Also request for me is under urllib3. Python 2.7.4 ubuntu
Riddle me this..
importing package disaster..why?
contents of yenlib:
/var/yenlib
- __init__.py
- yenlib
- __init__.py
- json_.py
import sys
sys.path.append('/var/yenlib')
sys.path.append('/var/yenlib/yenlib')
from yenlib import json_ as json
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name json_
Skip the second "append" statement and import from yenlib.yenlib
import sys
sys.path.append("/var/yenlib")
from yenlib.yenlib import json_ as json
I encoutner the following import resolution error in a program of mine:
I am: homie.translators.is24.rest.translator
Traceback (most recent call last):
File "/usr/local/sbin/is24rest", line 3, in <module>
from homie.interfaces.is24.rest import __main__
File "/usr/local/lib/python3.2/dist-packages/homie/interfaces/is24/rest/__init__.py", line 8, in <module>
from .exporter import Exporter
File "/usr/local/lib/python3.2/dist-packages/homie/interfaces/is24/rest/exporter.py", line 12, in <module>
from homie.translators.is24.rest.translator import Translator
File "/usr/local/lib/python3.2/dist-packages/homie/translators/is24/rest/translator.py", line 9, in <module>
from .factories.rest.restFactory import RestFactory
ImportError: No module named factories.rest.restFactory
The content of /usr/local/lib/python3.2/dist-packages/homie/translators/is24/rest/translator.py is
"""
Created on 18.06.2014
#author: Richard Neumann
"""
print('I am: ' + str(__name__))
from homie.translators.abc import Translator as T
from .factories.rest.restFactory import RestFactory
from .factories.openimmo.openimmoFactory import OpenImmoFactory
class Translator(T):
<snip>
Also, the relative module exists:
root#srv:/usr/src/is24-translator# ls /usr/local/lib/python3.2/dist-packages/homie/translators/is24/rest/factories/rest/
abc.py attachments __init__.py __pycache__ realestates restFactory.py
Why does python3 try to do an absolute import here?
I do only encounter this problem under Debian 7 with python 3.2.3.
Under Arch w/ python 3.4.1 it works just fine.
Am I missing something?
There was the __init__.py missing in .factories, which caused this error.
Took me a while to realize that.
For some reason I receive an ImportError every time I try to import a class from another file. Here's the github page for my project: https://github.com/wheelebin/mcnextbot
Here's the error that I'm receiving:
Traceback (most recent call last):
File "ircbot.py", line 36, in <module>
from test import mcnextlvl
ImportError: cannot import name mcnextlvl
Here your from test import something refers to the module test in <PYTHONPATH>/lib, not yourselves test.py, and there is no submodule/class mcnextlvl there. You should use from lib.test import mcnextlvl as #sgmart commented.
The __init__.py python file allow you can import your individual modules named 'test' from the lib package.
This question already has answers here:
Use 'import module' or 'from module import'?
(23 answers)
Closed 8 years ago.
I'm wondering if there's any difference between the code fragment
from urllib import request
and the fragment
import urllib.request
or if they are interchangeable. If they are interchangeable, which is the "standard"/"preferred" syntax (if there is one)?
It depends on how you want to access the import when you refer to it.
from urllib import request
# access request directly.
mine = request()
import urllib.request
# used as urllib.request
mine = urllib.request()
You can also alias things yourself when you import for simplicity or to avoid masking built ins:
from os import open as open_
# lets you use os.open without destroying the
# built in open() which returns file handles.
Many people have already explained about import vs from, so I want to try to explain a bit more under the hood, where the actual difference lies.
First of all, let me explain exactly what the basic import statements do.
import X
Imports the module X, and creates a reference to that module in the
current namespace. Then you need to define completed module path to
access a particular attribute or method from inside the module (e.g.:
X.name or X.attribute)
from X import *
Imports the module X, and creates references to all public objects
defined by that module in the current namespace (that is, everything
that doesn’t have a name starting with _) or whatever name
you mentioned.
Or, in other words, after you've run this statement, you can simply
use a plain (unqualified) name to refer to things defined in module X.
But X itself is not defined, so X.name doesn't work. And if name
was already defined, it is replaced by the new version. And if name in X is
changed to point to some other object, your module won’t notice.
This makes all names from the module available in the local namespace.
Now let's see what happens when we do import X.Y:
>>> import sys
>>> import os.path
Check sys.modules with name os and os.path:
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
Check globals() and locals() namespace dict with name os and os.path:
>>> globals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> locals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> globals()['os.path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'os.path'
>>>
From the above example, we found that only os is added to the local and global namespaces.
So, we should be able to use os:
>>> os
<module 'os' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> os.path
<module 'posixpath' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>
…but not path:
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
Once you delete the os from locals() namespace, you won't be able to access either os or os.path, even though they do exist in sys.modules:
>>> del locals()['os']
>>> os
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> os.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
Now let's look at from.
from
>>> import sys
>>> from os import path
Check sys.modules with name os and os.path:
>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
So sys.modules looks the same as it did when we imported using import name.
Okay. Let's check what it the locals() and globals() namespace dicts look like:
>>> globals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> locals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['os']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'os'
>>>
You can access by using path, but not by os.path:
>>> path
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> os.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
Let's delete 'path' from locals():
>>> del locals()['path']
>>> path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>
One final example using aliasing:
>>> from os import path as HELL_BOY
>>> locals()['HELL_BOY']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['HELL_BOY']
<module 'posixpath' from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>
And no path defined:
>>> globals()['path']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'path'
>>>
One pitfall about using from
When you import the same name from two different modules:
>>> import sys
>>> from os import stat
>>> locals()['stat']
<built-in function stat>
>>>
>>> stat
<built-in function stat>
Import stat from shutil again:
>>>
>>> from shutil import stat
>>> locals()['stat']
<module 'stat' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc'>
>>> stat
<module 'stat' from
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc'>
>>>
THE LAST IMPORT WILL WIN
There is a difference. In some cases, one of those will work and the other won't. Here is an example: say we have the following structure:
foo.py
mylib\
a.py
b.py
Now, I want to import b.py into a.py. And I want to import a.py to foo. How do I do this? Two statements, in a I write:
import b
In foo.py I write:
import mylib.a
Well, this will generate an ImportError when trying to run foo.py. The interpreter will complain about the import statement in a.py (import b) saying there is no module b. So how can one fix this? In such a situation, changing the import statement in a to import mylib.b
will not work since a and b are both in mylib. The solution here (or at least one solution) is to use absolute import:
from mylib import b
Source: Python: importing a module that imports a module
You are using Python3 were urllib in the package. Both forms are acceptable and no one form of import is preferred over the other. Sometimes when there are multiple package directories involved you may to use the former from x.y.z.a import s
In this particular case with urllib package, the second way import urllib.request and use of urllib.request is how standard library uniformly uses it.
In python 2.x at least you cannot do import urllib2.urlopen
You have to do from urllib2 import urlopen
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2.urlopen
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named urlopen
>>> import urllib.request
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named request
>>>
My main complaint with import urllib.request is that you can still reference urllib.parse even though it isn't imported.
>>> import urllib3.request
>>> urllib3.logging
<module 'logging' from '/usr/lib/python2.7/logging/__init__.pyc'>
Also request for me is under urllib3. Python 2.7.4 ubuntu