我在名为 tags.py
的 module
中定义了此函数:
def lastfm_artist_to_tags(artist):
tag_weight = {}
result = last.get_artist(artist).get_top_tags()
for tag in result:
tag_weight[str(tag.item.get_name())] = str(tag.weight)
tag_weight = {k: int(v) for k, v in tag_weight.items()}
return sorted(tag_weight.items(), key=lambda x: x[1], reverse=True)
tags.py
作为 main,已成功调用 with:
print lastfm_artist_to_tags('radiohead') //note string
但我正在尝试将上述函数导入到我的 playlist.py
中,如下所示:
#playlist.py
from api.lastfm.seeds.tags import *
class hardrock(rock):
def __init__(self,name,user):
rock.__init__(self, name, user)
# get tags for this artist
tags = lastfm_artist_to_tags(str(artist))
并通过命令行调用它:
if len(sys.argv) > 1:
artist_name = ' '.join(sys.argv[1:])
artist = get_artist(artist_name) //get_artist() returns json response from Spotify API
作者:$ python playlist.py radiohead
但我收到以下错误:
错误:找不到您提供的艺术家
我也尝试过传递lastfm_artist_to_tags(artist)
,但无济于事。
文件夹结构:
playlist.py
api/
__init__.py
lastfm/
__init__.py
seeds/
__init__.py
tags.py
我在这里缺少什么?
编辑:
print (artist_name)
和 print (str(artist_name))
从 SPotify API
返回相同的结果:
{u'genres': [u'alternative rock', u'britpop', u'indie rock', u'neo mellow', u'permanent wave', u'pop rock', u'rock'], u'name': u'Oasis', u'external_urls': {u'spotify': u'https://open.spotify.com/artist/2DaxqgrOhkeH0fpeiQq2f4'}, u'popularity': 77, u'uri': u'spotify:artist:2DaxqgrOhkeH0fpeiQq2f4', u'href': u'https://api.spotify.com/v1/artists/2DaxqgrOhkeH0fpeiQq2f4', u'followers': {u'total': 1114541, u'href': None}, u'images': [{u'url': u'https://i.scdn.co/image/2a8c10fe954e2038fb74251cba601a5594cc5878', u'width': 640, u'height': 640}, {u'url': u'https://i.scdn.co/image/87d18c79bbfdb1905bb202d200e1c191afc46aa5', u'width': 320, u'height': 320}, {u'url': u'https://i.scdn.co/image/b4d024ebb4863438b92a1b029bff7f9737263a57', u'width': 160, u'height': 160}], u'type': u'artist', u'id': u'2DaxqgrOhkeH0fpeiQq2f4'}
{u'genres': [u'alternative rock', u'britpop', u'indie rock', u'neo mellow', u'permanent wave', u'pop rock', u'rock'], u'name': u'Oasis', u'external_urls': {u'spotify': u'https://open.spotify.com/artist/2DaxqgrOhkeH0fpeiQq2f4'}, u'popularity': 77, u'uri': u'spotify:artist:2DaxqgrOhkeH0fpeiQq2f4', u'href': u'https://api.spotify.com/v1/artists/2DaxqgrOhkeH0fpeiQq2f4', u'followers': {u'total': 1114541, u'href': None}, u'images': [{u'url': u'https://i.scdn.co/image/2a8c10fe954e2038fb74251cba601a5594cc5878', u'width': 640, u'height': 640}, {u'url': u'https://i.scdn.co/image/87d18c79bbfdb1905bb202d200e1c191afc46aa5', u'width': 320, u'height': 320}, {u'url': u'https://i.scdn.co/image/b4d024ebb4863438b92a1b029bff7f9737263a57', u'width': 160, u'height': 160}], u'type': u'artist', u'id': u'2DaxqgrOhkeH0fpeiQq2f4'}
最佳答案
如果这个有效:
print lastfm_artist_to_tags('radiohead') //note string
这意味着函数 lastfm_artist_to_tags
正在运行,您应该在其他地方寻找问题。
您可能应该检查从另一个模块调用时是否确实将 'radiohead'
传递给同一函数。最简单的方法是打印参数:
if len(sys.argv) > 1:
artist_name = ' '.join(sys.argv[1:])
print(artist_name) # Add this line
artist = get_artist(artist_name)
print(artist) # Add this line
print(str(artist)) # Add this line
tags = lastfm_artist_to_tags(str(artist))
这应该为您提供一些有关代码问题的有用信息。如果您发现所有打印的变量都符合预期,那么我很高兴回顾一下。
关于Python - 通过命令行将参数传递给模块函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40733103/