简体   繁体   English

无法在 VS Code 中导入

[英]Unable to Import in VS Code

I'm new to python and I've been using VS code.我是 python 新手,我一直在使用 VS 代码。 Right now I'm working on a Thompson Sampling problem that requires numpy and matplotlib.现在我正在研究需要 numpy 和 matplotlib 的 Thompson Sampling 问题。 I've imported both libraries but VS code is giving the error unable to import.我已经导入了两个库,但是 VS 代码给出了无法导入的错误。 I know I have to install with PIP, and I have seen other solutions about changing the Python path through the json.我知道我必须使用 PIP 进行安装,并且我已经看到了有关通过 json 更改 Python 路径的其他解决方案。 but I'm afraid to do that because I don't want to mess up my text editor.但我害怕这样做,因为我不想弄乱我的文本编辑器。 Can someone walk me through what I am supposed to do?有人可以引导我完成我应该做的事情吗? I haven't seen any simple tutorials on setting up VS Code for python.我还没有看到任何关于为 python 设置 VS Code 的简单教程。 I'm hoping this will help other people too!我希望这也能帮助其他人! I have the extensions Python and CodeRunner installed in VS code.我在 VS 代码中安装了扩展 Python 和 CodeRunner。 Thank you!谢谢!

solution to this is make virtual environment for your project steps are as follows解决方案是为您的项目创建虚拟环境步骤如下
1.go to specifid directory you want to make project. 1.转到要制作项目的指定目录。
2.open cmd or teminal an type mkdir testProjectName . 2.打开 cmd 或终端类型mkdir testProjectName
3.type cd testProjectName . 3. 输入cd testProjectName
4.type virtualenv venv . 4. 输入virtualenv venv
5.type for windows source venv/scripts/activate 5.type for windows source venv/scripts/activate
type for linux or mac source venv/bin/activate键入 linux 或 mac source venv/bin/activate
6.type pip install python . 6.输入pip install python
7.type pip install numpy . 7.输入pip install numpy
8.type pip install matplotlib . 8. 输入pip install matplotlib
9.make your project 'testProjectName' dir or copy and paste you project here. 9. 将您的项目设为“testProjectName”目录或将您的项目复制并粘贴到此处。
Hope this helps.....希望这可以帮助.....

如果您使用的是 Python 3,那么问题很可能是您在安装 numpy 和 matplotlib 时使用了pip命令,该命令只会为 Python 2 安装它们。要为 Python 3 安装库,只需使用pip3命令代替pip

this happened to me when installing numpy but after i restarted VScode it fixed it's self.这发生在我安装 numpy 时,但在我重新启动 VScode 后,它修复了它的自我。 if that didn't work check that the package is installed with: pip list in the command prompt.如果这不起作用,请检查该软件包是否已安装:命令提示符中的 pip list。

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

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