ModuleNotFoundError using CaseReader in OpenMDAO - openmdao

I'm trying to use a recorded case in OpenMDAO contained in "my_file.db"
when i execute the following code:
import openmdao.api as om
cr = om.CaseReader('my_file.db')
I get the following error:
ModuleNotFoundError: No module named 'groups'
'groups' is a folder from the openMDAO code that I used to record the case and now I'm trying to import it from a different directory. How can I redefine the path for om.CaseReader to look for the modules it needs?

try setting your PYTHONPATH, as discussed here:
https://bic-berkeley.github.io/psych-214-fall-2016/using_pythonpath.html

Solved using:
import os
dirname = os.path.dirname(__file__)
import sys
sys.path.append( dirname )

Related

AttributeError: JSON

I´m new in coding and for streamlit
I wanted to add aggrid
But when i try to run my code i get a JSON error that i have tried to fix
How can i fix my Attributeerror JSON?
I have uninstalled and reinstalled
pip uninstall streamlit-aggrid
pip install streamlit-aggrid
Nothing helps:
From the code for Aggrid:
import pandas as pd
import streamlit as st
import streamlit.components.v1 as components
from st_aggrid import GridOptionsBuilder, AgGrid, GridUpdateMode, DataReturnMode
and the lines for Aggrid configuration:
# Define AgGrid configuration
gb = GridOptionsBuilder.from_dataframe(df_selection)
gb.configure_default_column(groupable=True, value=True, enableRowGroup=True, aggFunc='sum', editable=True)
gridOptions = gb.build()
# Render AgGrid
components.html(AgGrid(df_selection, gridOptions=gridOptions, width='100%', height='500px', data_return_mode=DataReturnMode.JSON).get_html(),height=800)

Cannot import specific function in Jupyter notebook, however, some function in the same "~.py" can be imported

I have utils.py and there are two functions: runrealcmd, mol_with_atom_index
If I try to import the two function with the following code:
from utils import mol_with_atom_index, runrealcmd
It fails to import runrealcmd. The error message is like below:
ImportError: cannot import name 'runrealcmd' from 'utils'
However, if I try to import only the mol_with_atom_index with the following code:
from utils import mol_with_atom_index
It successes. The function of mol_with_atom_index can be imported in Jupyter notebook.
However, the function of runrealcmd cannot be imported in Jupyter notebook although both of the two functions are in the same utils.py file.
ImportError: cannot import name 'runrealcmd' from 'utils'
utils.py
from subprocess import Popen, PIPE, STDOUT
from IPython.core.magic import register_line_magic
#register_line_magic
def runrealcmd(command):
# Display instantly: https://stackoverflow.com/questions/52545512/realtime-output-from-a-shell-command-in-jupyter-notebook
process = Popen(command, stdout=PIPE, shell=True, stderr=STDOUT, bufsize=1, close_fds=True)
for line in iter(process.stdout.readline, b''):
print(line.rstrip().decode('utf-8'))
process.stdout.close()
process.wait()
def mol_with_atom_index(mol):
for atom in mol.GetAtoms():
atom.SetAtomMapNum(atom.GetIdx())
return mol
(Jupyter notebook) If you want to import the same-named function (previously imported with the same name) from the other path, you should restart the kernel first.

from transformers import TFBertModel, BertConfig, BertTokenizerFast

I am having trouble importing TFBertModel, BertConfig, BertTokenizerFast. I tried the latest version of transformers, tokenizer==0.7.0, and transformers.modeling_bert but they do not seem to work. I get the error
from transformers import TFBertModel, BertConfig, BertTokenizerFast
ImportError: cannot import name 'TFBertModel' from 'transformers' (unknown location)
Any ideas for a fix? Thanks!
Do you have Tensorflow 2 installed? The model you are trying to import required Tensorflow

NameError: name 'np' is not defined

I try to import the following functions:
import numpy as np
def load_labels(path):
y = np.load(path)
return y
def print_sentence():
print("hi")
from a Jupyter notebook, with name "save_load" into another Jupyter notebook with the following code:
!pip install import-ipynb
import import_ipynb
import save_load
from save_load import load_labels, print_sentence
The function print_sentence works fine in the notebook, but with the function load_labels I receive the following error:
NameError: name 'np' is not defined
What could be the reason for this error? I've imported numpy as np in both notebooks.
In "save_load" instead of import numpy as np try import numpy, it worked for me.
You can try this:
import numpy as np
or
from numpy import *
I had the same problem when I was fixing my codes on VScode. Try saving the file. Then run it again.

Run preprocessor using nbconvert as a library

I would like to run nbconvert with a preprocessor that removes cells marked with the tag "skip". I am able to do this from the command line, but when I try to use the nbconvert API within a notebook I run into problems.
An example
Following the example in the documentation, I get a notebook to work with.
from urllib.request import urlopen
url = 'http://jakevdp.github.com/downloads/notebooks/XKCD_plots.ipynb'
response = urlopen(url).read().decode()
import nbformat
nb = nbformat.reads(response, as_version=4)
I'll modify one cell so it gets skipped in the output.
nb.cells[1].metadata = {'tags': ['skip']}
Command line
Saving the file, and then running nbconvert from the command line:
nbformat.write(nb, 'nb.ipynb')
%%bash
jupyter nbconvert --to latex \
--TagRemovePreprocessor.remove_cell_tags='{"skip"}' \
--TagRemovePreprocessor.enabled=True \
'nb.ipynb'
This works. The output nb.tex file does not contain the cell tagged "skip".
API
Now let's try it using the API instead. First, without any preprocessing:
import nbconvert
latex, _ = LatexExporter().from_notebook_node(nb)
print(latex[:25])
\documentclass[11pt]{arti
Again, no problem. The conversion is working.
Now, trying to use the same preprocessor I used from the command line:
from traitlets.config import Config
c = Config()
c.RemovePreprocessor.remove_cell_tags = ('skip',)
c.LatexExporter.preprocessors = ['TagRemovePreprocessor']
LatexExporter(config=c).from_notebook_node(nb)
This time, I get:
ModuleNotFoundError: No module named 'TagRemovePreprocessor'
As far as I can see, this code matches the code sample in the documentation, except that I'm using the Latex exporter instead of HTML. So why isn't it working?
For your particular case, I believe you can resolve the issue by changing: c.RemovePreprocessor.remove_cell_tags = ('skip',) ->
c.TagRemovePreprocessor.remove_cell_tags = ('skip',)
For the benefit of others that come across this thread as I did by searching
ModuleNotFoundError: No module named 'TagRemovePreprocessor'
There is an open issue with TagRemovePreprocessor that causes all exporters other than the HTMLExporter (and LatexExporter?) to automatically disable this preprocessor.
In my case, I was attempting to use the NotebookExporter and needed to explicitly enable the preprocessor and change the preprocessing level like so:
import json
from traitlets.config import Config
from nbconvert import NotebookExporter
import nbformat
c = Config()
c.TagRemovePreprocessor.enabled=True # Add line to enable the preprocessor
c.TagRemovePreprocessor.remove_cell_tags = ["del_cell"]
c.preprocessors = ['TagRemovePreprocessor'] # Was previously: c.NotebookExporter.preprocessors
nb_body, resources = NotebookExporter(config=c).from_filename('notebook.ipynb')
nbformat.write(nbformat.from_dict(json.loads(nb_body)),'stripped_notebook.ipynb',4)

Resources