Python - 通过命令行将参数传递给模块函数

标签 python parameter-passing

我在名为 tags.pymodule 中定义了此函数:

 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/

相关文章:

php - 将复选框 ID 传递给另一个 php 文件

java - 加载 FXML 时将参数传递给 Controller

java - Java中有参数树的实现吗?

java - 将值从 JPanel 类传递到 JFrame 类

c++ - 将可变参数列表传递给系统日志

python - Aerospike Python 客户端中的段错误

python - 将行添加到 pandas 数据框,其中列包含应该是另一行的附加值?

python - 训练、测试、验证拆分 Python。三件套

python - 在没有 pip/anaconda 的 Ubuntu 上安装 numpy

python 'module' 对象没有属性 'compile'