How to properly import scatter_matrix() function from pandas? - python

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/

Related

In Python, how to import all modules from a library in one line?

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.

Why does python import module imports when importing *

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.

Unable to import modules using [dot]module

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 :)

Can you import another script/module from a variable?

Do you always have to "hard-code" imports or could you techically import for example another script whose name is inside an variable? The desired behaviour is:
var_my_script_name = "my_script"
import var_my_script_name
You sure can! just use the module importlib:
import importlib
name_of_my_module_or_script="my_script"
my_module_object = importlib.import_module("my_script")
# Can also import built-in or third-party modules
np = importlib.import_module("numpy")

Python: Write function in module to import packages

I am trying to write a function, which is itself loaded, to quickly import a bunch of modules globally.
I thought that, essentially, loaded modules could be treated as variables so I tried:
def loadMods():
global np
import numpy as np
and when I loaded numpy (calling np) there was no problem.
What I then did was to create a separate .py file called loadTest containing
# loadTest module
# coding: utf-8
def loadMod():
global np
import numpy as np
Then attempted to import numpy using this .py file in python (2.7):
import loadTest
loadTest.loadMod()
but now when attempting calling np I get
File "<stdin>", line 1, in <module>
NameError: name 'np' is not defined
Why does this occur? Any help or alternative ways of doing this would be much appreciated. Thanks a bunch :)
Instead of making a function to do this, why not make another module? You could name it something like modules.py and put all of your imports in there:
import numpy as np
import os
import sys
...
Then, all you need to do is a wildcard import:
from modules import *
and everything will be made available.
You must first define np like that.
In loadTest:
np=None
In somewhere other
import loadTest
loadTest.loadMod()
np=loadTest.np

Categories