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 :)
Related
I have several modules, all stored in the same directory. Each requires a lengthy list of libraries (numpy, pandas, etc.).
I have created one module called library_function in which I have defined the following function:
def library_fun():
import numpy as np
import pandas as pd
print("test)
#etc. many more libraries
In my main module, I have the following code:
# import required modules
from library_function import library_fun
# bring in the required libraries; see separate module for this
library_fun()
# Bring in Production Data
production_data = pd.Series([1,2])
I get an error "name 'pd' is not defined".
I know that library_fun is running - I can see the "test" printout.
Is there a way to fix this?
import nibabel
nibabel.processing.resample_to_output(input_img, voxel_size)
AttributeError: module 'nibabel' has no attribute 'processing'
import nibabel
import nibabel.processing
nibabel.processing.resample_to_output(input_img, voxel_size)
Why does the first code fail but the second code work?
Expanding on #juanpa answer in the comments, you can simply consider these as two different modules.
For this
import nibabel
You get the error which suggests that this module does not have an attribute named processing
But for this
import nibabel.processing
It works fine since itself can be considered a module and thus means that processing is not an attribute of nibabel.
So it looks the code that you are trying to run only requires the 2nd import and not the first.
Hello everyone im currently learning python and i im having some problems importing modules and packages. Actually i think is more of a problem with vscode.
i have this package called "paquete" with a module (funciones) that i want to import to my "main" with some fuctions in it to test if it all works correctly but i still getting "emphasized items and unresolved-import" warnings.
but for some reason it works just fine.
is more of a annoying thing.
EDIT:
module with the function "funcion"
the warning that appears in the main folder "prueba" is "emphasized items"
i tried what u guys told me to do but it stills shows the warnings
As you are trying to import a specific function from module in python
You should use in this manner:
from paquete import funciones
If you want to import full module then use:
import paquete
I can't tell whats in the funciones file. But normally this yellow import lines are telling you that you import functions, which you dont use.
Try this instead if you only want
funcion
to be imported.
from paquete.funcions import funcion
This is also better because you import only the functions you need, not all of the functions you declared in the other file. Also all imports of the other file will be loaded into your file if you import with an asterix.
The issue is you are doing all of this from within a directory named prueba. If you changed the import to from prueba.paquete.funciones import * it should work after you add a __init__.py file to your prueba directory. The other option is to use a relative import: from .paquete.funciones import *.
But do note that using import * is strongly discouraged when you are not working within the REPL. It's much better to import to the module and then reference things off the module, e.g. from prueba.paquete import funciones, from .paquete import funciones, or import prueba.paquete.funciones. That way you know exactly where things in your code came from without having to read the top of your file.
pip3 intall "name"
Use Pycharm, rather than Vscode
I'm working on a Jupyter Notebook for my master's thesis and I'd like to keep it clean. I use a lot of functions to assign categories to groups of data.
Therefore, I've decided to put all those functions in a functions.py module which I import at the start of my notebook. My notebook has the following imports:
import sys
sys.path.append('../src/') # ugly hack to be able to import the functions module
import re
import numpy as np
import pandas as pd
import seaborn as sns
import functions as fn
One of my functions uses the "re" module for matching strings with regex. When I called the said function I get NameError: ("name 're' is not defined", 'occurred at index 0') so I figured I had to import re at the beginning of my functions.py file. This didn't change anything. So I even tried to put import re in the function body, but it wouldn't work either.
I have absolutely no idea why re doesn't work despite trying to import it everywhere.
Note: my functions worked correctly when I was defining and using them from the notebook so I know for certain it's not a bug in my function.
Solved my own issue, the answer is stupidly simple: Jupyter doesn't take into account any edits to an imported module even if you reimport it. If you make any changes to a module you have to shut down the kernel and restart it, import again and the edits will work.
In my particular case I had added import re to my functions.py but Jupyter didn't take it into account until I restarted the kernel.
In a notebook, you can use the importlib library and call importlib.reload(module) instead of restarting the kernel
I was trying to import the following function in Python 2.7
import scipy.signal.savgol_filter as sgolay
I received the following error:
ImportError: No module named savgol_filter
savgol_filter is a function, not a module, so the error makes some sense. My question then is, is it not possible to import, without the use of the word "from" anything besides a module?
In other words, the following works:
from scipy.signal import savgol_filter as sgolay
But in general, does the following "sub_part" need to be a module?
import my_module.sub_part as some_name
I've seen lots of writing suggesting "sub_part" does not need to be a module. Is there something tricky going on with scipy that is making this not work?
Thanks,
Jim
In general, if you do import thing, import thing.subthing, import thing.subthing.subsubthing, etc., the far-right thing needs to be a module. Only the from form allows importing things that aren't modules. If you want a definitive statement of the forms of the import statement and what it allows, the Python language reference explains it in great detail, but it's a pretty dense read.
import is for importing modules. If you are not importing modules then you should see if its an object and create an instance like this:
import scipy as sp
sgolay = scipy.signal.savgol_filter
#Other stuff to do...