I've seen countless issues here on stack about this but still can't figure out why I cant get mine to work. I have a 2 .ipynb files and I'm looking to import a class from one file to the other as follows:
CV_Screening_Interface:
from joblib import dump, load
import sys
import pandas as pd
import os
import import_ipynb
import docx
import readDocx ***(This is another ipynb file)***
from docx import Document
import string
model = load('model.joblib')
class CV:
def __init__(self,university,major,masters,company,certification,GPA):
self.university = university
self.major = major
self.masters = masters
self.company = company
self.certification = certification
self.GPA = GPA
#And a bunch of other functions
Now in FirstProgram
from tkinter import *
from tkinter import filedialog
from docx import Document
import io
import import_ipynb
import CV_Screening_Interface
#Till here works fine
When I try to import class CV I get an import error
from CV_Screening_Interface import CV
OR
test = CV_Screening_Interface.CV()
ImportError: cannot import name 'CV' from 'CV_Screening_Interface' (CV_Screening_Interface.ipynb)
I checked PYTHONPATH, I have an empty init.py in the directory already. What's weird is that importing the module works, but importing the class in the module doesn't.
Note in CV_Screening_Interface CV class works perfectly fine so I don't think there is any issue with it specifically. Probably need a if name="main" inside it?
Ok so this solved my problems:
I created a new ipynb called ClassFile.ipynb and it only contains the Class CV without importing any packages
Then i converted ClassFile.ipynb to ClassFile.py and imported it to FirstProgram and it worked.
Related
When I am running datasets_utils.py from '/usr/local/lib/python3.7/dist-packages/torchtext/data/datasets_utils.py' in Google Colab, the following error occurs even with the most updated versions of Python packages:
ImportError: cannot import name 'functional_datapipe' from 'torch.utils.data' (/usr/local/lib/python3.7/dist-packages/torch/utils/data/init.py)
Are there any solutions to solve such errors, as I could not find functional_datapipe even in the official torch.utils.data documentation? The following is excerpt from datasets._utils.py in the Google Colab environment
import functools
import inspect
import os
import io
import torch
from torchtext.utils import (
validate_file,
download_from_url,
extract_archive,
)
from torch.utils.data import functional_datapipe, IterDataPipe
from torch.utils.data.datapipes.utils.common import StreamWrapper
import codecs
It might be available only on torchdata.datapipes
I just wrote a small package containing a class to use on jupyter labs at work, however when I try to import and use the class which is dependent on numpy and matplotlib, it says that np is not defined.
Even though it is imported into the file I am importing the class into, and I imported it into the class file itself that I am importing, and the __init__.py file for the package. I just need the class to load some data into a np.array([]) via np.genfromtxt().
I have made one package before and didn't have any issues but I'm new so am obviously missing something.
Here's how I get the error:
pip install git+https://github.com/Elliot-Wadge/pl_class#egg=pl_class
import numpy as np
import matplotlib.pyplot as plt
import glob
from pl_data import pl_data as pl
PL_data = pl.PL_data
flist = glob.glob("data/*")
dictionary = PL_data(flist, skip_header = 4, unpack = True, delimiter = '\t')
error message
class file
I alredy have a problem.
Show cairosvg as undefined. I wish save Turtle object as png.
I already installed the canvasvg and cairosvg libraries, but even so I keep getting the error. I leave my code as a sample. Thank you
from turtle import *
import os
import shutil
import tempfile
import canvasvg
from canvasvg import *
screen=Screen()
setup (640,520,0,0)
title("T")
tracer(0,0)
encendido='#51D1F6'
apagado='#922B3E'
pant=Turtle(visible=False)
pant.hideturtle()
pant.pensize(4)
pant.penup()
pant.goto(110,0)
pant.pendown()
pant.pendown()
pant.fillcolor(encendido)
pant.begin_fill()
pant.goto(-400,-180)
pant.goto(-400,-190)
pant.goto(-110,-190)
pant.end_fill()
Y=7
gb=Y*4
agua=Turtle(visible=False)
agua.pensize(4)
agua.penup()
agua.goto(110,-200)
agua.pendown()
agua.fillcolor(encendido)
agua.begin_fill()
agua.goto(110,-200+gb)
agua.penup()
agua.goto(-110,-200+gb)
agua.pendown()
agua.goto(-110,-200)
agua.goto(110,-200)
agua.end_fill()
update()
ts=getscreen().getcanvas()
tmpdir=tempfile.mkdtemp()
tmpfile=os.path.join(tmpdir,'tmp.svg')
canvasvg.saveall(tmpfile,ts)
name="image4.png"
with open(tmpfile) as svg_input, open(name, 'wb')as png_output:
cairosvg.svg2png(bytestring=svg_input.read(),write_to=png_output)
shutil.rmtree(tmpdir)
I am trying to make use of the streamlit SessionState, when I import SessionState. I get the following error: ModuleNotFoundError: No module named 'SessionState'
when using he SessionState
Here is a snipnet of my code:
from multiprocessing import Process
import streamlit as st
import SessionState
import time
import os
import signal
st.sidebar.title("Controls")
start = st.sidebar.button("Start")
stop = st.sidebar.button("Stop")
state = SessionState.get(pid=None)
Has anyone encountered this and how did you fix it? There are no resources online
https://docs.streamlit.io/en/stable/changelog.html?highlight=SessionState#version-0-54-0
Seems like you have to download this gist and put it into your project in order to use SessionState
I am trying to import a custom function from another python file but keep getting an error NameError: name 'testme' is not defined. I confirmed that I am importing the file correctly according to this SO post and that the function is top level. What else can I try to fix this?
My main python file is:
import sys
import dbconn
#from dbconn import testme #<----did not work
dev=True
if(dev):
categId='528'
pollIds=[529,530,531]
else:
categId=str(sys.argv[1])
pollIds=[529,530,531]
df=testme(categIds)#callServer(categId,pollIds)
df
if(not categId.isdigit):
print('categ id fail. expected digit got: '+categId)
.....
and dbconn.py:
import pymysql #pip3 install PyMySQL
import pandas as pd
from scipy.stats.stats import pearsonr
from scipy import stats
def testme(categIds):
try:
df=categIds
except Exception as e:
print("broke")
return categIds
Not sure if it makes a difference but I am running the main python file from within a Jupyter notebook, and have a compiled version of dbconn.py in the same directory
In response to the suggestions I tried:
df=dbconn.testme(categIds)
got the error:
module 'dbconn' has no attribute 'testme'
You Have to follow these fox exact import
1)import <package>
2)import <module>
3)from <package> import <module or subpackage or object>
4)from <module> import <object>
in your case, you have tried
from dbconn import testme
you have to use only packages in from section and module in import section
like >>
from testme import dbconn