简体   繁体   English

PyCharm“没有名为 sys 的模块”

[英]PyCharm “no module named sys”

Using PyCharm community edition and Python 2.7,使用 PyCharm 社区版和 Python 2.7,

import traceback
import sys

No problem on the first line, which implies that I have pointed PyCharm correctly at the interpreter.第一行没问题,这意味着我已经正确地将 PyCharm 指向了解释器。 However, I get "no module named sys" at the second line (which is strange, as sys is a builtin - if you can find the interpreter, you have found sys).但是,我在第二行得到“没有名为 sys 的模块”(这很奇怪,因为 sys 是一个内置函数——如果你能找到解释器,你就找到了 sys)。

Any ideas?有什么想法吗?


[Update] to the commentators so far, thanks. [更新] 到目前为止的评论员,谢谢。

The IDE does not offer sys for auto-complete after import sy . IDE 不提供sys以在import sy后自动完成。

When I start the interpreter which the IDE uses form the command line, then import sys works.当我从命令行启动 IDE 使用的解释器时, import sys工作。

I have had this issue with PyCharm before.我以前在 PyCharm 上遇到过这个问题。 I find that reloading the interpreter fixes the issue.我发现重新加载解释器可以解决这个问题。

File -> Settings -> Project -> Project Interpreter文件 -> 设置 -> 项目 -> 项目解释器

Remove the interpreter and add it again.删除解释器并再次添加它。

Make sure you've chosen the right Interpreter path and check some other run settings.确保您选择了正确的解释器路径并检查其他一些运行设置。 Go to Run > Edit Configurations... and check, if some configurations seem to be wrong.转到Run > Edit Configurations...并检查,如果某些配置似乎是错误的。

EDIT: Under Linux, you have often Python 2 and 3 installed, be sure to pick the correct one.编辑:在 Linux 下,你经常安装 Python 2 和 3,一​​定要选择正确的。

I had this problem as well and was completely stymied until I tried the following:我也遇到了这个问题,并且在尝试以下操作之前完全受阻:

File -> 
Default Settings -> 
Project Interpreter
  dropdown (click the arrow) ->
Show All ->
  select your interpreter ->
Show Paths
  (bottom icon on right) ->
Add Path ->
  Enter path to libraries
  (in my case this was c:\Python35\Lib).

我遇到了这个问题,我通过更新文件 -> 默认设置 -> 项目解释器下的默认项目设置,然后在项目解释器的下拉列表中,选择与运行配置使用的解释器相同的解释器(在我的情况下为 python 3.6)

I don't know why, but PyCharm is reproving that it can't find quite a few modules, while being fine on others. 我不知道为什么,但是PyCharm正在证明它找不到很多模块,而在其他模块上却很好。

Despite that, it will run the code, so I will just live with the problem as I will be upgarding to Python 3.x shortly and hope that this minor inconvenience will disappear then. 尽管如此,它将运行代码,因此我将忍受这个问题,因为我将很快升级到Python 3.x,并希望这种不便之处将会消失。

PyCharm has virtual Enviroment and System Interpreter. PyCharm 具有虚拟环境和系统解释器。 Select system interpreter选择系统解释器

在此处输入图片说明

We actually no need of having "import sys" in pycharm.我们实际上不需要在 pycharm 中使用“import sys”。 You can find this in 'os' module.您可以在“os”模块中找到它。

For example,例如,

import os

Sys = os.sys

print("say , Good Bye")


Sys.exit(0)

print("Sorry, i am not exited")

I hope it will work.我希望它会起作用。

Maybe you're updating your PYTHONPATH variable in your .bashrc file?也许您正在更新 .bashrc 文件中的 PYTHONPATH 变量? It started working on, after comment PATH PATH in bash_profile.在 bash_profile 中注释 PATH PATH 之后,它开始工作。

File -> Settings -> Project: (your_project_name) -> Project Interpreter -> press the + symbol -> search for 'system' and install package.文件 -> 设置 -> 项目:(your_project_name) -> 项目解释器 -> 按 + 符号 -> 搜索“系统”并安装包。

This solved the problem for me.这为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM