When I import module urllib in cmd I have only magic fuctions but when I import this module from Spyder I have all attribute.
I'm adding printscreen of this.
Why can not I import all attribute?
To import all attributes of a module into the global namespace use from module import *.
While dir() simply lists the attributes of the global namespace, dir(module) lists the ones in the scope of the specific module.
Anyway, when you import urllib you can still reach all attributes if you specify the module name, e.g. urllib.request.
Related
I have a module (say module A) that for one of its functions, returns a BeautifulSoup object. I am writing a second module (module B) that calls this function and stores that BeautifulSoup object. I am confused how I can call BeautifulSoup functions on the object that was returned by module A in module B without module B importing anything from bs4, or having to access those BS4 functions through module A. Is the import basically putting module_a and all of its imports in the package, and so the BeautifulSoup class is visible to module_b?
module_a.py
from bs4 import BeautifulSoup
def function():
some_xml = "<name>Namespaces are strange.</name>"
return BeautifulSoup(some_xml, "xml")
module_b.py
import module_a
def main():
# How does this line know what to do with .find()? or .string?
print(module_a.function().find("name").string)
This is the beauty of a dynamic language like python. module_a.function() tells python to:
go to module a
lookup a function called "function" and call it
take the returned object, lookup "find" and call it
take the returned object, lookup "string" and call it
Since all of these lookups happen dynamically as the function or method is called, module_b doesn't have to have a predefined interface for bs4 or string. Its just looking for a method called "find" that could come from anywhere. In fact, module_a could swap in some other implementation and as long as the methods being called are still there, it still works.
from bs4 import BeautifulSoup imports the bs4 module (and any submodules it imports) into python. You can see the module in sys.modules when its done. It then reaches into bs4, looks up BeautifulSoup and adds that class to module_a 's namespace. The module is now available to all other imported modules... its just that they don't know about it because they haven't imported it. Modules that just use the resulting objects from bs4 never need to see it directly.
Python has complicated namespaces and modules notion, so i unsure about this. Normally python module and something that is imported from it has different names or only module is imported and it's content used by fully qualified name:
import copy # will use copy.copy
from time import localtime # "localtime" has different name from "time".
But what if module has same name as something that i'm importing from it? For example:
from copy import copy
copy( "something" )
Is it safe? Maybe it's some complicated consequences that i can't see?
From PEP8 ( http://www.python.org/dev/peps/pep-0008/#imports):
When importing a class from a class-containing module, it's usually okay to spell this:
from myclass import MyClass
from foo.bar.yourclass import YourClass
If this spelling causes local name clashes, then spell them
import myclass
import foo.bar.yourclass
and use "myclass.MyClass" and "foo.bar.yourclass.YourClass".
I'm trying to import just a variable inside a class from another module:
import module.class.variable
# ImportError: No module named class.variable
from module.class import variable
# ImportError: No module named class
from module import class.variable
# SyntaxError: invalid syntax (the . is highlighted)
I can do the following, but I'd prefer to just import the one variable I need.
from module import class as tmp
new_variable_name = tmp.variable
del tmp
Is this possible?
variable = __import__('module').class.variable
You can't do that -
the import statement can only bring elements from modules or submodules - class attributes, although addressable with the same dot syntax that is used for sub-module access, can't be individually imported.
What you can do is:
from mymodule import myclass
myvar = myclass.myvar
del myclass
Either way, whenever one does use the from module import anything syntax, the whole module is read and processed.The exception is
from module.submodule import submodule
, where, if thesubmodule itself does not require the whole module, only the submodule is processed.
(So, even onmy workaround above , mymodule is read, and executed - it is not made accessible in the global namespace where the import statement is made, but it will be visible, with all its components, in the sys.modules dictionary.
Follow these two steps
Import the class from the module
from Module import Class
Assign new variables to the existing variables from the class
var1=Class.var1
var2=Class.var2
I have a GP.py file that I am then running a MyBot.py file from.
In the MyBot.py file, I have the line
from GP import *
I have a suspicion it is importing the whole file instead of just the class methods and class descriptions I want. In the GP.py file, There is code in addition to the defintions
You cannot import class methods separately, you have to import the classes. You can do this by enumerating the classes you want to import:
from GP import class1, class2, class3
Note that this will still load the entire module. This always happens if you import anything from the module. If you have code in that module that you do not want to be executed when the module is imported, you can protect it like this:
if __name__ == "__main__":
# put code here
Code inside the block will only be executed if the module is run directly, not if it is imported.
_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an
underscore.
Use this instead:
from GP import SomeClass
Have a look at PEP-8 (Python Guidelines) if you want to use import *
Modules that are designed for use via from M import * should use the
__all__ mechanism to prevent exporting globals
It is not recommended to import all from a module. Zen of Python says "Explicit is better than Implicit"
It may have some side effects by overriding an existing name. You should always keep control on the namespace.
You can import your classes and function this way:
from GP import MyClass, my_function
An alternative is to import the module itself
import GP
GP.my_function()
GP.MyClass()
This way, you create a namespace for the GP module and avoid to overwrite something.
I hope it helps
import * indeed import all classes, functions,variables and etc..
if you want to import only specific class use
from GP import class_name
and as far as i know you cannot import only class methods
If you want to import just some methods from class
from GP.MyClass import MyFunction
When you import a module, then reimport it again, will it get reimported/overwritten, or skipped?
When you import module "a" and "b", but also have module "b" imported in module "a", what happens? Is it safe to do this? For example if that module "b" has a class instantiated in it, will you end up instantiating it twice?
import loads the matching .py, .pyc or .pyo file, creates a module object, and stores it with its fully qualified ("dotted") name in the sys.modules dictionary. If a second import finds the module to import in this dictionary, it will return it without loading the file again.
To answer your questions:
When you import a module, then reimport it again, will it get reimported/overwritten, or skipped?
It will get skipped. To explicitely re-import a module, use the reload() built-in function.
When you import module "a" and "b", but also have module "b" imported in module "a", what happens?
import a will load a from a.py[c], import b will return the module sys.modules['b'] already loaded by a.
Is it safe to do this?
Yes, absolutely.
For example if that module "b" has a class instantiated in it, will you end up instantiating it twice?
Nope.
The module will only be instantiated once. It is safe to import the same module in multiple other modules. If there is a class instance (object) that is created in the module itself, the very same object will be accessed from all modules that import it.
You can, if you like, have a look at all imported modules:
import sys
print sys.modules
sys.modules is dictionary which maps module names the module objects. The first thing the import statement does is looking in sys.modules, if it cannot find the module there, it will be instantiated, and added to sys.modules for future imports.
See this page for more details:
http://effbot.org/zone/import-confusion.htm (see "What Does Python Do to Import a Module?")