在intellij中导入新python模块的最佳方法是什么? [英] What is the best way to import new python modules in intellij?

查看:2170
本文介绍了在intellij中导入新python模块的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已经阅读了这里列出的答案,并尝试按照列出的说明这里,但说明是针对过时的或至少是针对不同版本的Intellij,并且预先存在的SO答案描述了问题,但至少对我来说没有提供解决方案。考虑到这一点:

To start, I've read the answer listed here, as well as tried to follow the instructions listed here, but the instructions were for an outdated or at least for a different version of Intellij, and the preexisting SO answer described the problem, but at least for me did not provide a solution. With that in mind:

我在Windows上使用IntellijIdea 2017.3。我正在尝试在Python 3中创建一个基本的Web scraper(我在这方面很新,所以我提前道歉)。为了实现这一点,我想使用我使用pip成功安装的BeautifulSoup库:

I'm using IntellijIdea 2017.3 on Windows. I'm trying to create a basic web scraper in Python 3 (I'm very new at this, so I apologize in advance). To accomplish this, I want to use the library BeautifulSoup, which I've successfully installed using pip:

以下是我的项目层次结构:

Here's what my project hierarchy looks like:

当然,当我尝试在scraper.py中导入beautifulsoup时,它会告诉我没有这样的模块。我链接到上面的堆栈溢出帖子告诉我这是因为pip安装到\python\python36-32 \lib \site-packages中,但我的Python解释器在另一个目录中。但是我该如何解决这个问题呢?我是否将目录pip installs更改为,是否要更改项目解释器的位置?有没有办法直接通过Intellij安装python库而不使用上面过时的PyCharm指令?我一般都是python的新手并且非常困惑。

Of course, when I try to import beautifulsoup in scraper.py, it tells me there's no such module. The stack overflow post I linked to above tells me that this is because pip installs into \python\python36-32\lib\site-packages, but my Python interpreter is in another directory. But how do I fix this? Do I change the directory pip installs into, do I change where my project interpreter is? Is there a way to install python libraries directly through Intellij without using the outdated PyCharm instructions above? I'm new to python in general and very confused.

如果它有帮助,这就是我的项目设置,模块设置和SDK配置的样子:

In case it helps, here's what my project settings, module settings, and SDK configuration look like:



推荐答案

您为系统范围的解释程序安装了模块。但是,您为IntelliJ项目创建了一个虚拟环境。要在此虚拟环境中从命令行安装模块,必须先将其激活。为此,请在IntelliJ中打开终端并运行activate命令。

You installed the module for the system-wide interpreter. However, you created a virtual environment for your IntelliJ project. In order to install modules from the command line in this virtual environment, you must activate it first. To do so, open the terminal in IntelliJ and run the activate command.

在Windows上:

venv/Scripts/activate 

在Linux和Mac上:

On Linux and mac:

. venv/bin/activate

现在你可以运行

pip install beautifulsoup4

或者,您可以手动输入在.py文件中导入:

Alternatively, you can manually type an import in a .py file:

from bs4 import BeautifulSoup

IntelliJ会抱怨没有名为 bs4 的模块。将光标移动到此名称,然后按Alt-Enter。应该有一个安装模块的选项。由于IntelliJ已经知道虚拟环境,因此它会将模块安装在正确的位置,以便您的项目可用。

IntelliJ will complain that there is no module named bs4. Move the cursor to this name and press Alt-Enter. There should be an option to install the module. Since IntelliJ is already aware of the virtual environment, it will install the module in the correct location so that your project will have it available.

这篇关于在intellij中导入新python模块的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆