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.
Related
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.
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 )
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
I am trying to use a Python block in my R markdown document. Below is what I have been trying:
"""{r setup, include=FALSE}
require(reticulate)
use_python("/Users/hyunjindominiquecho/opt/anaconda3", required = T)
"""
"""{python}
# import the necessary python packages
import numpy as np
import pandas as pd
import scipy.stats as st
from rpy2 import robjects
import math
from scipy.optimize import newton
import torch
from pandas import dataframe
from statistics import mean
"""
but when I try to do this, R studio shows the following error:
Error in system2(command = python, args = paste0("\"", config_script, :
error in running command
How can I resolve this issue? Thank you,
What if you did:
```{python, engine.path = '/Users/hyunjindominiquecho/opt/anaconda3'}
import sys
print(sys.version)
```
What happens when you only run the first chunk? Do you still get the same error?
I am attempting to convert some pandas dataframes to R objects for use in rpy2.
I am getting an error when i try to import the conversion the module. from rpy2.robjects import pandas2ri yields ImportError: cannot import name 're_type' as follows:
/opt/conda/lib/python3.6/site-packages/pandas/core/dtypes/inference.py in <module>()
6 from collections import Iterable
7 from numbers import Number
----> 8 from pandas.compat import (PY2, string_types, text_type,
9 string_and_binary_types, re_type)
10 from pandas._libs import lib
ImportError: cannot import name 're_type'
I haven't really seen any discussion of this error elsewhere.
in terms of dependencies, i am using pandas 0.23.4, rpy2 2.9.4, R 3.4, and it is running on the docker jupyter container datascience-notebook
Really hoping someone could help me here!