V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
QGabriel
V2EX  ›  Python

Python 小白提个问题

  •  
  •   QGabriel · 2020-12-24 10:58:54 +08:00 · 2939 次点击
    这是一个创建于 1190 天前的主题,其中的信息可能已经有所发展或是发生改变。
    pip3 list

    Package Version
    ---------------- ---------
    beautifulsoup4 4.9.3
    bs4 0.0.1
    certifi 2020.12.5
    chardet 4.0.0
    idna 2.10
    lxml 4.6.2
    pip 20.3.3
    requests 2.25.1
    setuptools 49.2.1
    simplejson 3.17.2
    six 1.15.0
    soupsieve 2.1
    tushare 1.2.62
    urllib3 1.26.2
    websocket-client 0.57.0

    执行 python index.py
    Traceback (most recent call last):
    File "index.py", line 2, in <module>
    import tushare as ts
    ImportError: No module named tushare

    我已经安装了 tushare 为什么还说没有 tushare 呢
    18 条回复    2020-12-25 12:11:48 +08:00
    jxxz
        1
    jxxz  
       2020-12-24 11:01:19 +08:00   ❤️ 1
    python3 index.py
    shyrock
        2
    shyrock  
       2020-12-24 11:01:50 +08:00
    估计是 2.x 和 3.x 两个环境。试试 python3 index.py
    lifetimeporn
        3
    lifetimeporn  
       2020-12-24 11:07:18 +08:00
    直接原因,就是你安装了 tushare 这个包的 python 环境,与你运行 index.py 的代码的 python 环境不是同一个环境。
    SjwNo1
        4
    SjwNo1  
       2020-12-24 11:10:45 +08:00
    你的包装在了 python3 下
    linw1995
        5
    linw1995  
       2020-12-24 11:19:30 +08:00
    pip --version 可以看到 package 会安装到哪个 python
    QGabriel
        6
    QGabriel  
    OP
       2020-12-24 12:05:13 +08:00
    @shyrock 确实有 2.7 和 3.8 版本 怎么把 2.7 删了?或者默认执行 3.8 版本?
    no1xsyzy
        7
    no1xsyzy  
       2020-12-24 12:07:53 +08:00
    @QGabriel 得看你系统,可能删 2.7 导致系统整体崩溃。
    推荐是用 virtualenv 隔离开每个项目环境。
    yanbc
        8
    yanbc  
       2020-12-24 12:13:46 +08:00 via iPhone   ❤️ 1
    装 python 包的时候正确姿势是 python -m pip install,尽量避免直接 pip install 。这样你用的是哪个环境中的 python binary 它就会装在哪个环境
    perpetually
        9
    perpetually  
       2020-12-24 13:47:03 +08:00
    你都说了 pip3 list
    怎么不用 python3 index.py
    XIVN1987
        10
    XIVN1987  
       2020-12-24 13:48:54 +08:00
    @QGabriel
    index.py 的第一行添加“#! python3”,执行的时候用“py index.py”,,py.exe 会自动根据文件第一行选择用 python2 执行还是用 python3 执行
    这样写还有一个好处,双击 index.py 执行的时候系统也能够自动选择正确版本的 python
    arischow
        11
    arischow  
       2020-12-24 13:50:24 +08:00
    如果可以的话,在你的 index.py 第一行加上 #!/usr/bin/env python3
    UN2758
        12
    UN2758  
       2020-12-24 14:43:25 +08:00
    linux 系统?看看 which python 执行的是哪个位置的 python
    ShuoHui
        13
    ShuoHui  
       2020-12-24 14:45:05 +08:00 via iPhone
    用虚拟环境吧
    aristolochic
        14
    aristolochic  
       2020-12-24 15:25:20 +08:00
    @no1xsyzy 不论是开发还是测试生产,都别碰系统的。Python 更是如此,一个几乎所有发行版都得乖乖放到 base 里面的超级依赖,疯了才去卸载它( Arch Linux 躲过一劫)。剩下的不要碰系统 Python 的原因是,系统的包管理器也会打包 Python 包,这时候如果用 Pip 更新了,会检查文件完整性的系统包管理器(比如 pacman )就会直接罢工。这个 Nodejs 啊 Ruby 啊等等也是一样的。推荐的做法是一定要用虚拟环境,个人的解决方案是把系统 Python 的所有依赖交给系统包管理器管理,甚至 pip 能不装都别装,平常用的话,用系统包管理器装的 pipx 装 PyPi 发行的 cli 工具,比如 pgcli 、asciinema 之类的,然后再装个 poetry 负责平常的开发
    learningman
        15
    learningman  
       2020-12-24 17:03:54 +08:00
    @aristolochic poetry 的 IDE 集成太差了,然后那个 toml 手写又老错
    frostming
        16
    frostming  
       2020-12-24 17:04:46 +08:00
    no1xsyzy
        17
    no1xsyzy  
       2020-12-24 20:22:56 +08:00
    @aristolochic 这就是特别好用的脚本语言通常最终会发生的事儿(
    不过你是不是回复错人了(
    aristolochic
        18
    aristolochic  
       2020-12-25 12:11:48 +08:00
    @no1xsyzy yep ( x
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5455 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 08:51 · PVG 16:51 · LAX 01:51 · JFK 04:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.