gpt4 book ai didi

ImportError: No module named dateutil.parser(ImportError:没有名为Dateutil.parser的模块)

转载 作者:bug小助手 更新时间:2023-10-25 14:34:43 42 4
gpt4 key购买 nike



I am receiving the following error when importing pandas in a Python program

当我在一个Python程序中导入熊猫时,我收到以下错误



monas-mbp:book mona$ sudo pip install python-dateutil
Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
Cleaning up...
monas-mbp:book mona$ python t1.py
No module named dateutil.parser
Traceback (most recent call last):
File "t1.py", line 4, in <module>
import pandas as pd
File "/Library/Python/2.7/site-packages/pandas/__init__.py", line 6, in <module>
from . import hashtable, tslib, lib
File "tslib.pyx", line 31, in init pandas.tslib (pandas/tslib.c:48782)
ImportError: No module named dateutil.parser


Also here's the program:

这里也是程序:



import codecs 
from math import sqrt
import numpy as np
import pandas as pd

users = {"Angelica": {"Blues Traveler": 3.5, "Broken Bells": 2.0,
"Norah Jones": 4.5, "Phoenix": 5.0,
"Slightly Stoopid": 1.5,
"The Strokes": 2.5, "Vampire Weekend": 2.0},

"Bill":{"Blues Traveler": 2.0, "Broken Bells": 3.5,
"Deadmau5": 4.0, "Phoenix": 2.0,
"Slightly Stoopid": 3.5, "Vampire Weekend": 3.0},

"Chan": {"Blues Traveler": 5.0, "Broken Bells": 1.0,
"Deadmau5": 1.0, "Norah Jones": 3.0, "Phoenix": 5,
"Slightly Stoopid": 1.0},

"Dan": {"Blues Traveler": 3.0, "Broken Bells": 4.0,
"Deadmau5": 4.5, "Phoenix": 3.0,
"Slightly Stoopid": 4.5, "The Strokes": 4.0,
"Vampire Weekend": 2.0},

"Hailey": {"Broken Bells": 4.0, "Deadmau5": 1.0,
"Norah Jones": 4.0, "The Strokes": 4.0,
"Vampire Weekend": 1.0},

"Jordyn": {"Broken Bells": 4.5, "Deadmau5": 4.0,
"Norah Jones": 5.0, "Phoenix": 5.0,
"Slightly Stoopid": 4.5, "The Strokes": 4.0,
"Vampire Weekend": 4.0},

"Sam": {"Blues Traveler": 5.0, "Broken Bells": 2.0,
"Norah Jones": 3.0, "Phoenix": 5.0,
"Slightly Stoopid": 4.0, "The Strokes": 5.0},

"Veronica": {"Blues Traveler": 3.0, "Norah Jones": 5.0,
"Phoenix": 4.0, "Slightly Stoopid": 2.5,
"The Strokes": 3.0}
}



class recommender:

def __init__(self, data, k=1, metric='pearson', n=5):
""" initialize recommender
currently, if data is dictionary the recommender is initialized
to it.
For all other data types of data, no initialization occurs
k is the k value for k nearest neighbor
metric is which distance formula to use
n is the maximum number of recommendations to make"""
self.k = k
self.n = n
self.username2id = {}
self.userid2name = {}
self.productid2name = {}
# for some reason I want to save the name of the metric
self.metric = metric
if self.metric == 'pearson':
self.fn = self.pearson
#
# if data is dictionary set recommender data to it
#
if type(data).__name__ == 'dict':
self.data = data

def convertProductID2name(self, id):
"""Given product id number return product name"""
if id in self.productid2name:
return self.productid2name[id]
else:
return id


def userRatings(self, id, n):
"""Return n top ratings for user with id"""
print ("Ratings for " + self.userid2name[id])
ratings = self.data[id]
print(len(ratings))
ratings = list(ratings.items())
ratings = [(self.convertProductID2name(k), v)
for (k, v) in ratings]
# finally sort and return
ratings.sort(key=lambda artistTuple: artistTuple[1],
reverse = True)
ratings = ratings[:n]
for rating in ratings:
print("%s\t%i" % (rating[0], rating[1]))




def loadBookDB(self, path=''):
"""loads the BX book dataset. Path is where the BX files are
located"""
self.data = {}
i = 0
#
# First load book ratings into self.data
#
f = codecs.open(path + "BX-Book-Ratings.csv", 'r', 'utf8')
for line in f:
i += 1
#separate line into fields
fields = line.split(';')
user = fields[0].strip('"')
book = fields[1].strip('"')
rating = int(fields[2].strip().strip('"'))
if user in self.data:
currentRatings = self.data[user]
else:
currentRatings = {}
currentRatings[book] = rating
self.data[user] = currentRatings
f.close()
#
# Now load books into self.productid2name
# Books contains isbn, title, and author among other fields
#
f = codecs.open(path + "BX-Books.csv", 'r', 'utf8')
for line in f:
i += 1
#separate line into fields
fields = line.split(';')
isbn = fields[0].strip('"')
title = fields[1].strip('"')
author = fields[2].strip().strip('"')
title = title + ' by ' + author
self.productid2name[isbn] = title
f.close()
#
# Now load user info into both self.userid2name and
# self.username2id
#
f = codecs.open(path + "BX-Users.csv", 'r', 'utf8')
for line in f:
i += 1
#print(line)
#separate line into fields
fields = line.split(';')
userid = fields[0].strip('"')
location = fields[1].strip('"')
if len(fields) > 3:
age = fields[2].strip().strip('"')
else:
age = 'NULL'
if age != 'NULL':
value = location + ' (age: ' + age + ')'
else:
value = location
self.userid2name[userid] = value
self.username2id[location] = userid
f.close()
print(i)


def pearson(self, rating1, rating2):
sum_xy = 0
sum_x = 0
sum_y = 0
sum_x2 = 0
sum_y2 = 0
n = 0
for key in rating1:
if key in rating2:
n += 1
x = rating1[key]
y = rating2[key]
sum_xy += x * y
sum_x += x
sum_y += y
sum_x2 += pow(x, 2)
sum_y2 += pow(y, 2)
if n == 0:
return 0
# now compute denominator
denominator = (sqrt(sum_x2 - pow(sum_x, 2) / n)
* sqrt(sum_y2 - pow(sum_y, 2) / n))
if denominator == 0:
return 0
else:
return (sum_xy - (sum_x * sum_y) / n) / denominator


def computeNearestNeighbor(self, username):
"""creates a sorted list of users based on their distance to
username"""
distances = []
for instance in self.data:
if instance != username:
distance = self.fn(self.data[username],
self.data[instance])
distances.append((instance, distance))
# sort based on distance -- closest first
distances.sort(key=lambda artistTuple: artistTuple[1],
reverse=True)
return distances

def recommend(self, user):
"""Give list of recommendations"""
recommendations = {}
# first get list of users ordered by nearness
nearest = self.computeNearestNeighbor(user)
#
# now get the ratings for the user
#
userRatings = self.data[user]
#
# determine the total distance
totalDistance = 0.0
for i in range(self.k):
totalDistance += nearest[i][1]
# now iterate through the k nearest neighbors
# accumulating their ratings
for i in range(self.k):
# compute slice of pie
weight = nearest[i][1] / totalDistance
# get the name of the person
name = nearest[i][0]
# get the ratings for this person
neighborRatings = self.data[name]
# get the name of the person
# now find bands neighbor rated that user didn't
for artist in neighborRatings:
if not artist in userRatings:
if artist not in recommendations:
recommendations[artist] = (neighborRatings[artist]
* weight)
else:
recommendations[artist] = (recommendations[artist]
+ neighborRatings[artist]
* weight)
# now make list from dictionary
recommendations = list(recommendations.items())
recommendations = [(self.convertProductID2name(k), v)
for (k, v) in recommendations]
# finally sort and return
recommendations.sort(key=lambda artistTuple: artistTuple[1],
reverse = True)
# Return the first n items
return recommendations[:self.n]

r = recommender(users)
# The author implementation
r.loadBookDB('/Users/mona/Downloads/BX-Dump/')

ratings = pd.read_csv('/Users/danialt/BX-CSV-Dump/BX-Book-Ratings.csv', sep=";", quotechar="\"", escapechar="\\")
books = pd.read_csv('/Users/danialt/BX-CSV-Dump/BX-Books.csv', sep=";", quotechar="\"", escapechar="\\")
users = pd.read_csv('/Users/danialt/BX-CSV-Dump/BX-Users.csv', sep=";", quotechar="\"", escapechar="\\")



pivot_rating = ratings.pivot(index='User-ID', columns='ISBN', values='Book-Rating')

更多回答

use --upgrade to upgrade - did you try that? It looks like your dateutil may be out of date.

使用--升级来升级--你试过了吗?看起来你的Dateutil可能已经过期了。

perhaps try force reinstalling sudo pip install python-dateutil --force-reinstall... Also relevant is how did you install pandas?

或许可以尝试强制重新安装sudo pip Install python-datutil--force-reinstall...同样相关的是你是如何安装熊猫的?

@AndyHayden I solved the problem in the answer. However I am dealing with a new problem describe in the answer.

@AndyHayden我解决了答案中的问题。然而,我正在处理答案中描述的一个新问题。

When it's solved, and not useful to others, consider deleting the question. Instead of posting a new question in your "answer", consider posting a new question... at least if it is of general interest to others, too!

当这个问题得到解决,并且对其他人没有用处时,考虑删除这个问题。与其在你的“答案”中发布新问题,不如考虑发布一个新问题。至少,如果其他人也对它感兴趣的话!

sudo pip install numpy python-dateutil pytz pyparsing six --force-reinstall --upgrade finally did it for me (I was about to get mad)

Sudo pip INSTALL NAMPY PYTHON-DATUTIL PYTZ PYPARSING Six--force-reinstall--upgrading终于为我做到了(我都快疯了)

优秀答案推荐

On Ubuntu you may need to install the package manager pip first:

在Ubuntu上,您可能需要首先安装包管理器pip:



sudo apt-get install python-pip


Then install the python-dateutil package with:

然后使用以下命令安装python-datutil包:



sudo pip install python-dateutil


For Python 3:

对于Python3:



pip3 install python-dateutil


You can find the dateutil package at https://pypi.python.org/pypi/python-dateutil. Extract it to somewhere and run the command:

您可以在https://pypi.python.org/pypi/python-dateutil.上找到Dateutil包将其解压缩到某个位置并运行命令:



python setup.py install

Python setup.py安装



It worked for me!

这对我很管用!



I have same issues on my MacOS and it's work for me to try

我在我的MacOS上遇到了同样的问题,对我来说,尝试


pip3 install python-dateutil

PIP3安装Python-Dateutil


on Ubuntu

在Ubuntu上


sudo apt-get install python-dateutil

SUDO APT-GET INSTALL PYSTON-DATUTIL


Check here



If you're using a virtualenv, make sure that you are running pip from within the virtualenv.

如果您使用的是Virtualenv,请确保您正在从Virtualenv中运行pip。



$ which pip
/Library/Frameworks/Python.framework/Versions/Current/bin/pip
$ find . -name pip -print
./flask/bin/pip
./flask/lib/python2.7/site-packages/pip
$ ./flask/bin/pip install python-dateutil


None of the solutions worked for me. If you are using PIP do:

所有的解决方案都没有对我起作用。如果您使用的是PIP,请执行以下操作:



pip install pycrypto==2.6.1

PIP安装加密==2.6.1



In Ubuntu 18.04 for Python2:

在适用于Python2的Ubuntu 18.04中:



sudo apt-get install python-dateutil


For me, the problem was with my Pandas installation. This might not be a solution for everyone, but in particular if you have this problem:

对我来说,问题出在我的熊猫安装上。这可能不是每个人的解决方案,但特别是如果您有这个问题:


File "/home/cc/.local/lib/python3.8/site-packages/pandas/core/dtypes/cast.py", line 23, in <module>
from dateutil.parser import ParserError
ImportError: cannot import name 'ParserError' from 'dateutil.parser' (/usr/lib/python3/dist-packages/dateutil/parser/__init__.py)

Or something along those lines, then this could be the solution:

或者类似的东西,那么这可能就是解决方案:


pip3 uninstall pandas
pip3 install pandas

This is assuming Python 3.

这是假设使用的是Python3。



I had the similar problem. This is the stack trace:

我也有过类似的问题。这是堆栈跟踪:


Traceback (most recent call last):
File "/usr/local/bin/aws", line 19, in <module> import awscli.clidriver
File "/usr/local/lib/python2.7/dist-packages/awscli/clidriver.py", line 17, in <module> import botocore.session
File "/usr/local/lib/python2.7/dist-packages/botocore/session.py", line 30, in <module> import botocore.credentials
File "/usr/local/lib/python2.7/dist-packages/botocore/credentials.py", line 27, in <module> from dateutil.parser import parse
ImportError: No module named dateutil.parser

I tried to (re-)install dateutil.parser through all possible ways. It was unsuccessful.

我尝试通过所有可能的方法(重新)安装Dateutil.parser。但这并不成功。


I solved it with

我解决了这个问题


pip3 uninstall awscli
pip3 install awscli


sudo python3 -m pip install PyPDF2 passlib babel werkzeug lxml decorator polib pillow psycopg2 idna python-dateutil psutil requests jinja2


If you are using Pipenv, you may need to add this to your Pipfile:

如果你使用的是Pipenv,你可能需要在你的Pipfile中添加以下内容:



[packages]
python-dateutil = "*"

更多回答

It is recommended to do pip install without sudo

建议在不使用sudo的情况下执行pip安装

@MikeL Just FYI - I tried the pip install without the sudo on my RPI and it didn't work. OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/dateutil'

@Mikel只是仅供参考-我在RPI上尝试了没有sudo的pip安装,但没有起作用。操作系统错误:[Errno 13]权限被拒绝:‘/usr/local/lib/python2.7/dist-packages/dateutil’

If you've always used sudo, your packages are installed to the system (/usr/...). This is a bad idea, but workable if you are the only user of the system. If not, they're installed for your user (/home/yourname/...). However, the recommendation to use virtualenvs rather than installing to the system keeps getting stronger. See packaging.python.org/tutorials/installing-packages for more detail.

如果您一直使用sudo,则您的包将安装到系统中(/usr/...)。这不是一个好主意,但如果你是该系统的唯一用户,这是可行的。如果没有,则为您的用户安装它们(/home/you name/...)。然而,使用Virtualenv而不是安装到系统上的建议越来越强烈。有关更多细节,请参见packaging.python.org/tutorials/installing-packages。

good they put in python-dateutil instead of dateutil -- it increases traffic to stack-overflow and stops people from confusing it with java-dateutil dotnet-dateutil which can also be installed using pip...

很好,他们放入了python-datutil而不是datutil--它增加了堆栈溢出的通信量,并防止人们将其与也可以使用pip安装的Java-datutil-Dotnet-datutil混淆……

thanks. it's annoying that the package has "python-" in front. like duh, as opposed to javascript?

谢谢。令人讨厌的是,包的前面有“python-”。就像duh,而不是javascrip?

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com