【问题标题】:Module can be imported in python but cannot be used from command line模块可以在 python 中导入,但不能从命令行使用
【发布时间】:2021-04-08 03:31:19
【问题描述】:

这与这个问题本质上是相反的:Import module works in terminal but not in IDLE

如果我启动一个 python 会话并尝试导入烧瓶,那么它工作得很好。尽管如此,如果我只是从终端运行flask,它不会被识别。

我已经确认pip 安装到的位置在我的系统路径上。 (我在 Windows 10 上,我在 Powershell 中输入了 $Env:Path 以查看,例如,C:\Users\Zack\AppData\Roaming\Python\Python37\site-packages 在系统路径中。)

我也确定我在虚拟环境中做了pip install --user flask不是。当我运行import flask; print(flask.__file__ 时,我看到C:\Users\Zack\AppData\Roaming\Python\Python37\site-packages)

对于可能出现的问题还有其他想法吗?

编辑:我正在尝试运行flask run。要检查 python 使用的路径,我做了(在 python 中)import sys; for i in sys.path: print(i)

【问题讨论】:

  • 你确认主模块中的shebank实际上使用了你路径上的同一个python吗?
  • @LhasaDad 我可能误会了,你指的是哪个shebang?我自己没有写任何 shebang,因为到目前为止我一直在通过 CLI 运行 python
  • 你是如何运行程序的。您可以将您指定的命令行添加到帖子(通过编辑)吗?另外,您使用的是 windows cmd shell 还是 powershell?
  • shebang 是 unix 的东西。您可以让文件的第一行有一个注释,有效地说明用于运行程序的可执行文件。

标签: python windows path package command-line-interface


【解决方案1】:

作为基于Python解释器的Python语言,在使用Python之前,必须先激活Python env。

【讨论】:

  • 这是一个不具体的答案,不会对问题添加任何内容。
猜你喜欢
  • 2014-12-13
  • 2021-12-23
  • 2018-04-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
相关资源
最近更新 更多