简体   繁体   English

导入模块在 PyCharm 中工作,但在终端 python 3.7 中出错

[英]Import module works in PyCharm but giving error in terminal python 3.7

I have project where I have created multiple python files based on its usage.我有一个项目,我根据它的使用情况创建了多个 python 文件。 It works completely fine when I run from the pycharm.当我从 pycharm 运行时,它完全正常。 However, when I run the same from the terminal, I get the error: ModuleNotFoundError: No module named 'dataflow'但是,当我从终端运行相同的程序时,出现错误:ModuleNotFoundError: No module named 'dataflow'

I need to make the dataflow out of this and need to deploy and it is giving an error while doing so.我需要从中制作数据流并需要进行部署,但这样做时出现错误。

Folder structure of the project, this works when I run from PyCharm项目的文件夹结构,这在我从 PyCharm 运行时有效

在此处输入图片说明

Error while running it from the terminal从终端运行时出错

在此处输入图片说明

Adducated guess, runs pychar your code in venv too?添加猜测,是否也在 venv 中运行 pychar 您的代码? If not you might check if you installed the package that is missing in your venv.如果没有,您可以检查是否安装了 venv 中缺少的软件包。

Update更新

if you intent to have a dataflow package that you want to import and use the modules in it you need a __init__.py file in your dataflow folder.如果您打算拥有一个要导入并使用其中的模块的数据流包,则您的数据流文件夹中需要一个 __init__.py 文件。 this makes it a package for python.这使它成为python的一个包。 If you want to use the modules in dataflow with the .如果要将数据流中的模块与 . in an import you need to do an import in __init__.py在导入中,您需要在 __init__.py 中进行导入

like so像这样

import .driver_main

this makes stuff from driver_main available in dataflow but better practice would be to specifiy what you want to access from driver_main like这使得来自 driver_main 的东西在数据流中可用,但更好的做法是指定您想从 driver_main 访问的内容,例如

from .driver_main import MyDriver

this will gibe you access to my driver via这将使您可以通过以下方式访问我的驱动程序

dataflow.MyDriver

If you really just want to acces stuff from one module on the same lvl you should able to do so with the same approach.如果您真的只想访问同一 lvl 上的一个模块中的内容,您应该能够使用相同的方法来实现。 so in you exaple you showed in the picture try to change所以在你的例子中你在图片中显示尝试改变

from dataflow import driver_main

to

   from . import driver_main

this would apply to an import in a module on the same lvl as driver_main.py like app.py这将适用于与 driver_main.py 相同 lvl 的模块中的导入,如 app.py

Update on Comments in original post更新原始帖子中的评论

btw the env in pychar has nothing to do with the venv in the console.顺便说一句,pychar 中的 env 与控制台中的 venv 无关。 You simply telling pycharm to use python 3.7 but with your venv you copy binaries in a folder stucture.您只需告诉 pycharm 使用 python 3.7,但使用 venv 将二进制文件复制到文件夹结构中。 that said if you run an virtual environment all stuff you pip there gets copied in this folder structure not in the global site-packages.也就是说,如果你运行一个虚拟环境,你在那里 pip 的所有东西都会被复制到这个文件夹结构中,而不是在全局站点包中。 This means if you installed stuff global you wont have it right away in the virtual env and the other way around!这意味着如果你安装了全局的东西,你不会马上在虚拟环境中拥有它,反之亦然!

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

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