I ran the following code as well as different version of same code but I keep running into this specific error: **ModuleNotFoundError: No module named 'pandas.core.internals.managers'; 'pandas.core.internals' is not a package
**
Please see code below:
import pickle
pickle_off = open(r"C:\Users\database.pkl","rb")
df = pd.read_pickle(pickle_off)
Let me know if this works:
import pickle
file_name = 'data.p'
with open(file_name,mode='rb') as f:
data = pickle.load(f)
Related
When I try to use Python to save a certificate in the PEM format, I get this import error: ImportError: cannot import name 'Encoding' from 'cryptography.hazmat.primitives.serialization.base'. I don't know why this is, because I have cryptography in my requirements.txt.
I'm using it in this function, I've also tried to make this function without using Encoding, but I couldn't find how:
def save_to_pem(cert, outpath, encoding=Encoding.PEM):
if outpath is not None:
pem_bytes = cert.public_bytes(encoding)
print(f'Saving to file {outpath} ...')
with open(outpath, 'wb+') as f:
f.write(pem_bytes)
I'm at a total loss here so any help is appreciated. Thanks.
In my Django project I have the following directory structure:
project/build/contracts/MyFile.json
And I am writing code in the following directory
project/homeapp/views.py
In my views.py I have the following code:
with open("../build/contracts/MyFile.json", "r") as f:
data = json.loads(f.read())
abi = data["abi"]
When I try to python manage.py runserver I get the following error:
The strange part is that I couldn't figure out what was wrong so I made a viewstest.py and placed the exact same code in it. When I run it with python .\viewstest.py and print the JSON to console, it works perfectly fine.
I even tried importing the abi variable from viewstest.py to views.py but got the same error. So I assume that it is an error relating to Django, but I can't seem to figure it out.
Thanks!
It should be json.load() and not json.loads()
Change the following code to:
with open("../build/contracts/MyFile.json", "r") as file:
data = json.load(file)
abi = data["abi"]
Edit:
Another alternative to get the path correct can be to use Pathlib.
from pathlib import Path
def your_view_func(request):
current_dir = Path.cwd()
file_loc = 'build/contracts/MyFile.json'
with open(current_dir.joinpath(file_loc), 'r') as file:
data = json.load(file)
abi = data["abi"]
So an update, I found my compile issue was that I needed to change my notebook to a py file and choosing save as doesn't do that. So I had to run a different script turn my notebook to a py file. And part of my exe issue was I was using the fopen command that apparently isn't useable when compiled into a exe. So I redid the code to what is above. But now I get a write error when trying to run the script. I can not find anything on write functions with os is there somewhere else I should look?
Original code:
import requests
import json
import pandas as pd
import csv
from pathlib import Path
response = requests.get('url', headers={'CERT': 'cert'}, stream=True).json()
json2 = json.dumps(response)
f = open('data.json', 'r+')
f.write(json2)
f.close()
Path altered code:
import requests
import json
import pandas as pd
import csv
from pathlib import Path
response = requests.get('url', headers={'CERT': 'cert'}, stream=True).json()
json2 = json.dumps(response)
filename = 'data.json'
if '_MEIPASS2' in os.environ:
filename = os.path.join(os.environ['_MEIPASS2'], filename)
fd = open(filename, 'r+')
fd.write(json2)
fd.close()
The changes to the code allowed me to get past the fopen issue but created a write issue. Any ideas?
If you want to write to a file, you have to open it as writable.
fd = open(filename, 'wb')
Although I don't know why you're opening it in binary if you're writing text.
So I am trying to get data from NFLfastR and my R equivalent code is:
data <- readRDS(url('https://raw.githubusercontent.com/guga31bb/nflfastR-data/master/data/play_by_play_2019.rds'))
data
I have previously tried pyreadr module as well but that did not work for me. Currently I am using rpy2 module to make it work. Here is the code I am trying:
import rpy2.robjects as robjects
from rpy2.robjects import pandas2ri
import os
os.environ["R_HOME"] = r"C:\Program Files\R\R-3.6.3"
os.environ["PATH"] = r"C:\Program Files\R\R-3.6.3\bin\x64" + ";" + os.environ["PATH"]
pandas2ri.activate()
readRDS = robjects.r['readRDS']
df = readRDS(url('https://raw.githubusercontent.com/guga31bb/nflfastR-data/master/data/play_by_play_2019.rds'))
df = pandas2ri.ri2py(df)
Rds and Rdata files are difficult to read in other languages than R as the format although open is undocumented. Therefore there are not many options on how to read them in python. One is what you propose. Another is to use pyreadr, but you have to download the file to disk first as pyreadr cannot read directly from an url:
import pyreadr
from urllib.request import urlopen
link="https://raw.githubusercontent.com/guga31bb/nflfastR-data/master/data/play_by_play_2019.rds"
response = urlopen(link)
content = response.read()
fhandle = open( 'play_by_play_2019.rds', 'wb')
fhandle.write(content)
fhandle.close()
result = pyreadr.read_r("play_by_play_2019.rds")
print(result.keys())
EDIT
pyreadr 0.3.7 includes now a function to download files:
import pyreadr
url = "https://github.com/hadley/nycflights13/blob/master/data/airlines.rda?raw=true"
dst_path = "/some/path/on/disk/airlines.rda"
res = pyreadr.read_r(pyreadr.download_file(url, dst_path), dst_path)
In R, unlike Python, you do not have to qualify every function with its package source unless you face name conflicts. Additionally in R, there is no built-in method. Every function you call resides in a package. But R ships with default packages such as utils, base, stats for routine methods.
Specifically, your working R code calls two functions from base package as shown with double colon aliases:
nfl_url <- 'https://raw.githubusercontent.com/guga31bb/nflfastR-data/master/data/play_by_play_2019.rds'
data <- base::readRDS(base::url(NFL))
data
Consequently, you need to run the analogous procedure in Python's rpy2 by explicitly importing base package:
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri
base = importr("base")
nfl_url <- 'https://raw.githubusercontent.com/guga31bb/nflfastR-data/master/data/play_by_play_2019.rds'
r_df <- base.readRDS(base.url(nfl_url))
pandas2ri.activate()
py_df = pandas2ri.ri2py(r_df)
Well if you just want to read nflFastR data, you can directly read it in python as follows:
import pandas as pd
pd.read_csv('https://github.com/guga31bb/nflfastR-data/blob/master/data/' \
'play_by_play_2019.csv.gz?raw=True',
compression='gzip', low_memory=False)
But as of now, there is not a way to do this via python. Its hard enough to read an on-premises (.rds) file while reading from a url is something I never saw implemented. So you have to download the file on-premises then you can have read it directly using pyreadr package or rpy2 (if you have R installed) as you mentioned.
i am trying to run a file "reader_microphone.py" with some of its line below
import pyaudio
import numpy
import wave
from reader import basereader
class MicrophoneReader(BaseReader):
default_chunksize = 8192
default_format = pyaudio.paInt16
default_channels = 2
default_rate = 44100
default_seconds = 0
i am getting this error-
from reader import basereader
ImportError: cannot import name 'basereader' from 'reader' (C:\ProgramData\Anaconda3\lib\reader.py)
and welcome to StackOverflow.
There is no basereader reference in the reader package. You can take a look at the package's documentation here, to try and see what's the object you are looking for.