在python中用来引入模块的是,在intellij中导入新python模块的最佳方法是什么?

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:

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:

8RydV.png

Here's what my project hierarchy looks like:

AQs4a.png

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.

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

9NXUb.png

jREnT.png

GXvRg.png

解决方案

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.

On Windows:

venv/Scripts/activate

On Linux and mac:

. venv/bin/activate

Now you can run

pip install beautifulsoup4

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

from bs4 import BeautifulSoup

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值