简体   繁体   English

无法在 Python 中导入包

[英]Unable to import a package in Python

I am new to Python Development and I tried to create a very simple API using Bottle Web Framework.我是 Python 开发的新手,我尝试使用 Bottle Web Framework 创建一个非常简单的 API。 And I got the following error: "Unable to import 'bottle'pylint(import-error)".我收到以下错误:“无法导入‘瓶’pylint(导入错误)”。

I am using Python 3.8.6 with pipenv.我正在使用带有 pipenv 的 Python 3.8.6。

The commands I runned were the following:我运行的命令如下:

pipenv shell

pipenv install bottle

The code in the main.py file is as follows: main.py文件中的代码如下:

from bottle import get, run

@get('/')
def index():
    return 'Hello World'

run(host='localhost', port=7819)

To run the main.py file, use the following command:要运行 main.py 文件,请使用以下命令:

python3 main.py

But the previous command only works if I have the work environment running (pipenv shell).但是前面的命令仅在我运行工作环境(pipenv shell)时才有效。

I tried to disable the Python Linting (which I didn't want to do because it helps us) and I changed the interpreter several times.我试图禁用 Python Linting(我不想这样做,因为它对我们有帮助)并且我多次更改了解释器。

And the error I always have is this:我一直遇到的错误是:

Traceback (most recent call last):

File "main.py", line 1, in <module>

from bottle import get, run

ModuleNotFoundError: No module named 'bottle'

I don't know if it has anything to do with it but I installed pipenv with HomeBrew and Pylint was installed as an VS Code extension.我不知道它是否与它有关,但我用 HomeBrew 安装了 pipenv,而 Pylint 被安装为 VS Code 扩展。

Thank you guys, I am just tired of trying so many different solutions and the error persists.谢谢你们,我只是厌倦了尝试这么多不同的解决方案,但错误仍然存​​在。 😞 😞

Heyo, it seems you are doing pipenv install bottler even though you are trying to use bottle in your code. Heyo,看来你是做pipenv install bottler即使你试图使用bottle在你的代码。 Instead, try running pipenv install bottle , this can be a potential fix, so please let me know if this has helped!相反,尝试运行pipenv install bottle ,这可能是一个潜在的修复,所以如果这有帮助,请告诉我!


find the script file open cmd and change directory to your script and run pip install bottle.找到脚本文件打开 cmd 并将目录更改为您的脚本并运行 pip install bottle。 and pip install shell It worked for me和 pip install shell 它对我有用

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

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