简体   繁体   中英

What is the best way to import new python modules in intellij?

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:在此处输入图片说明

Here's what my project hierarchy looks like:

在此处输入图片说明

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:

在此处输入图片说明在此处输入图片说明在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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