终端找不到PyCharm中导入的模块 [英] Imported modules in PyCharm not found by the terminal

查看:148
本文介绍了终端找不到PyCharm中导入的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有三个.py文件.一个是file2.py,它会导入googlesearch,硒等.另一个是file3.py,它会随机导入.最后一个是我的main.py,它导入了file2.py和file3.py.

I have three .py files in my projects. One is a file2.py which imports googlesearch, selenium etc. Other is file3.py which imports random. And the last one is my main.py which imports file2.py and file3.py.

在PyCharm中,我的程序可以正常运行,并且正确安装了每个模块,但是当我尝试从终端运行main.py时,它给了我并出现错误,找不到模块"googlesearch".可能模块本身并不重要,它恰好是我的程序中第一个使用的模块,因此会出错.

In PyCharm my program works just fine, with every module installed correctly, but when I try to run my main.py from the terminal it gives me and error that module 'googlesearch' is not found. Probably the module itself doesn't matter, it just happens to be the first used in my program and so the error.

当我尝试将项目保存到.exe时,我发现了此问题,该程序将很快出现并消失.我设法减慢了速度,并给了我同样的错误.

I found this issue when trying to save my project to .exe and the program would just appear and disappear rapidly. I managed to slow it down and it gives me the same error.

我的项目文件如下所示:

My project file looks something like this:

.idea - folder
__pychace__ - folder
venv - folder 
.google-cookie
file2.py
main.py

在我的venv文件夹中,我有:

In my venv folder I have:

__pycache__ - folder
Include - folder
Lib (which has all my packages and modules) - folder
Scripts - folder
file3.py
pyvenv.cfg

现在我真的不明白为什么在我的venv文件中有file3.py.而且我不知道如何使我的exe文件检测到我导入的模块.我以某种方式推测未检测到我的lib.我是一个初学者,如果你们有什么想法,我将非常感谢.

Now I don't really understand why file3.py is in my venv. And I don't know how can I make my exe file detect my imported modules. I presume somehow that my lib is not detected. I am quite a beginner and if you guys have any ideas I would really appreciate it.

推荐答案

由于我曾经拥有PyCharm作为Python的IDE,因此我完全理解了您的问题.

I understand your problem completely as I once had PyCharm as the IDE for Python.

PyCharm的工作原理如下:

So here is how PyCharm works:

  • 在PyCharm中创建新项目时,它会自动为该项目创建虚拟环境.

  • When you create a new project in PyCharm, it automatically creates a virtual environment for the project.

创建一个名为 venv 的文件夹(如您所述).

A folder named venv is created (as you mentioned).

在此文件夹中可以看到一个子文件夹,即 libs .这是当前活动项目中所有这些模块的安装文件夹.这意味着您安装的模块不会安装在Python的 PATH 中,而是会下载到项目专用的 venv 文件夹的库中.

In this folder a sub folder namely libs can be seen. This is the folder in which all those modules are installed for the currently active project. This means that the modules you install are not getting installed in the Python's PATH but being downloaded in the venv folder's library which is exclusive for the project.

现在我希望这个解释很清楚.

Now I hope this explanation is clear.

什么解决方案?

这很简单.安装Python时,它将直接添加到 PATH 中,这意味着您可以使用 Command Prompt 轻松访问它.要在不使用PyCharm和IDLE或其他任何方式的情况下访问模块,您需要在Python的make目录中安装模块,该目录使用 CMD 下载到 PATH 无需反复安装或使用PyCharm即可淘汰任何模块.

It is very simple. When you install Python, it is directly added to PATH which means you can easily access it using Command Prompt. To access the modules without using PyCharm and using IDLE or whatever else, you need install modules in the Python's make directory that is downloading using CMD to the PATH from where Python can take out any module without installing again and again or using PyCharm.

步骤:

  1. 检查是否将Python添加到 PATH .很简单.打开 CMD ,然后键入 python ,然后按Enter.您会在 CMD 中看到Python的交互模式.如果看不到该错误,而是看到此错误:

  1. Check if Python is added to PATH. It is simple. Open CMD and type python and press enter. You would see the Python's Interactive mode in CMD. If you do not see that and instead see this error:

'python' is not recognized as an internal or external command,
operable program or batch file.

我建议您参考以下站点将 Python 添加到 PATH :

I recommend you to add Python to PATH with reference to the site: https://www.educative.io/edpresso/how-to-add-python-to-path-variable-in-windows

第二步是执行pip安装命令.(仅适用于添加到PATH的Python.请检查点1以作参考.)命令:

The second step is to execute a pip installation command. (Would only work with Python added to PATH. Check point 1 for reference.) The command:

py -m pip install <module name>

添加要安装的软件包的名称,然后将安装该模块.

Add the name of the package you want to install and the module would be installed.

现在,您可以在任何需要的地方使用该特定模块,而无需使用PyCharm.

Now you would be able to use that particular module any where you want without using PyCharm.

个人建议::您使用VS Code而不是PyCharm,因为它比PyCharm更轻巧,更快捷,并且实际上不会为您带来此类错误.同样,在VS Code中,您可以选择激活虚拟环境.是否使用它取决于您在做什么.

Personal Advice: You VS Code instead of PyCharm as it is lighter and faster than PyCharm and doesn't really bother you with such errors. Also in VS Code you get a choice to activate a Virtual Environment. You can use it or not depending on what you are doing.

这篇关于终端找不到PyCharm中导入的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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