why does 'from sklearn.impute import SimpleImputer as si' works but '
import sklearn.impute.SimpleImputer as si'
do not work
I want to know, why this won't work. I am new to python.
The reason for this is the way the Python import statement works. The first import statement imports the SimpleImputer class from the sklearn.impute module and then names it si. The second import statement tries to import a module named SimpleImputer from a module named sklearn.impute. This does not work because in Python, the import statement only allows you to import modules, not submodules.
You can only use import with modules.
with from ... import ... you can import variables so submodules, functions, classes, and everything else.
As SimpleImputer is not a module only the second option is availiable.
Writen a bit differently import only works in general with files, from ... import works with variables declared in the script.
Related
The following code works:
import torch
import pytorch_lightning as torchl
import pytorch_forecasting as torchf
from pytorch_forecasting.data.examples import get_stallion_data
x = torchf.data.examples.get_stallion_data()
print(x)
However, if I remove the 4th line, I get an error:
AttributeError: module 'pytorch_forecasting.data' has no attribute 'examples'
It is tedious and ugly to devote many lines just for loading several modules from one library, like:
from pytorch_lightning.callbacks import EarlyStopping, LearningRateMonitor
from pytorch_lightning.loggers import TensorBoardLogger
from pytorch_forecasting import Baseline, TemporalFusionTransformer, TimeSeriesDataSet
from pytorch_forecasting.data import GroupNormalizer
from pytorch_forecasting.metrics import SMAPE, PoissonLoss, QuantileLoss
from pytorch_forecasting.models.temporal_fusion_transformer.tuning import optimize_hyperparameters
from pytorch_forecasting.data.examples import get_stallion_data
Is there a way to import all these modules using just one line, and then later use them as:
x=torchf.bla.bla.bla.bla(parameters)
I prefer this way also because there is less danger of name conflicts: calling x=library1.module(); y=library2.module(); avoids name clashes, unlike from library1 import *; from library2 import *; x=module(); y=module();.
To import everything from a package you can just do import pytorch_lightning and it will import all modules and files inside the package. Then you can use it as x = pytorch_lightning.callbacks.EarlyStopping().
The drawback is that you are loading files and modules(and packages that they import consecutively) that you do not necessarily require which increases the import time.
Let's say I have a file where I'm importing some packages:
# myfile.py
import os
import re
import pathlib
def func(x, y):
print(x, y)
If I go into another file and enter
from myfile import *
Not only does it import func, but it also imports os, re, and pathlib,
but I DO NOT want those modules to be imported when I do import *.
Why is it importing the other packages I'm importing and how do you avoid this?
The reason
Because import imports every name in the namespace. If something has a name inside the module, then it's valid to be exported.
How to avoid
First of all, you should almost never be using import *. It's almost always clearer code to either import the specific methods/variables you're trying to use (from module import func), or to import the whole module and access methods/variables via dot notation (import module; ...; module.func()).
That said, if you must use import * from module, there are a few ways to prevent certain names from being exported from module:
Names starting with _ will not be imported by import * from .... They can still be imported directly (i.e. from module import _name), but not automatically. This means you can rename your imports so that they don't get exported, e.g. import os as _os. However, this also means that your entire code in that module has to refer to the _os instead of os, so you may have to modify lots of code.
If a module contains the name __all__: List[str], then import * will export only the names contained in that list. In your example, add the line __all__ = ['func'] to your myfile.py, and then import * will only import func. See also this answer.
from myfile import func
Here is the fix :)
When you import *, you import everything from. Which includes what yu imported in the file your source.
It has actually been discussed on Medium, but for simplification, I will answer it myself.
from <module/package> import * is a way to import all the names we can get in that specific module/package. Usually, everyone doesn't actually use import * for this reason, and rather sticked with import <module>.
Python's import essentially just runs the file you point it to import (it's not quite that but close enough). So if you import a module it will also import all the things the module imports. If you want to import only specific functions within the module, try:
from myfile import func
...which would import only myfile.func() instead of the other things as well.
It does not see the scatter_matrix, says "not found":
import pandas.plotting
scatter_matrix(df[['col1', 'col2']])
I tried this:
import pandas.plotting.scatter_matrix
ImportError: No module named 'pandas.plotting.scatter_matrix'
actually when I hit tab after "plotting." the popup hint for import is showing some functions to import but no scatter_matrix
But doing this works and plots:
pandas.plotting.scatter_matrix(df[['col1', 'col2']])
Why do i Need the whole path to use scatter_matrix? How to import the scatter_matrix?
Simply change it from
import pandas.plotting.scatter_matrix
to
from pandas.plotting import scatter_matrix
When you're importing you import a file, if you want to import specific function you must specify from what file you want to import it (:
Edit:
There are generally two types of import syntax. When you use the first one, you import the resource directly, like this:
import abc
abc can be a package or a module.
When you use the second syntax, you import the resource from another package or module. Here’s an example:
from abc import xyz
xyz can be a module, subpackage, or object, such as a class or function.
reference- https://realpython.com/absolute-vs-relative-python-imports/
I'm trying to make use of some parts of the Source code for statsmodels.iolib.summary2. If I try to run the source code in its entirety I'm getting the error:
ModuleNotFoundError: No module named 'main.table'; 'main' is not a package
This is raised after:
from .table import SimpleTable
From the post What does a . in an import statement in Python mean? I'm able to understand why this error is raised conceptually (at best), but I have no Idea how to work around it.
What I've tried:
From the comments we have:
[...] It basically means the current namespace or package directory
This made me think that it was imported from textwrap, but it doesn't seem that way. Since statsmodels is often imported like import statsmodels.api as sm i thought that it would be a similar thing with Simpletable, so I've tried importing it in different ways and with different combinations of statsmodels and statsmodels.compat.collections since these are also used in the beginning of the source:
from statsmodels.compat.python import (lrange, iterkeys, iteritems, lzip,
reduce, itervalues, zip, string_types,
range)
from statsmodels.compat.collections import OrderedDict
import numpy as np
import pandas as pd
import datetime
import textwrap
from .table import SimpleTable
from .tableformatting import fmt_latex, fmt_txt
I've also tried to install SimpleTable (conda), but I think I'm just way off target here, so any suggestions would be great!
Try from statsmodels.iolib.table import SimpleTable instead if you've already installed statsmodel module via pip/conda. It's a class defined inside statsmodels.iolib.table package/file :)
The error says that the there is no module named as "gtts.gTTS", and got this error many times while importing other modules. So can you brief me what is the logic behind importing modules? Can't we import class using "." operator?
What is the problem; I can't understand!
#what is the difference between these two codes
import gtts.gTTS
from gtts import gTTS
Unlike Java, where you do something like import module.submodule.blah.blah.MyClass, in Python, you can only directly import modules. If you want to only import a certain class, function, or other named value from a module, you need to use the from ... import ... syntax.
In all likelihood, gtts is a module, and gTTS is a class within that module. Therefore, import gtts.gTTS makes no sense, since gTTS isn't a module (that's what the error says), you must use from gtts import gTTS
For example, import os.path works fine, since path is a submodule of os, but if I wanted to use the exists function in path, I would need to use from os.path import exists or import os.path; os.path.exists(...). I get a ModuleNotFoundError if I erroneously try import os.path.exsts.