简体   繁体   English

在命令行上运行python脚本失败

[英]Running python script on command line fails

I'm trying to run a python script on my Windows 10 Computer. 我正在尝试在Windows 10计算机上运行python脚本。 The structure of the directory looks as follows 目录的结构如下

MyProject
   - src
      - main.py
      __init__.py

      - features
          - featureExtraction.py
          - __init__.py

The main folder is "MyProject". 主文件夹是“ MyProject”。 In "MyProject" is a subfolder called "src". 在“ MyProject”中是一个名为“ src”的子文件夹。 In "src" the files "main.py" and " init .py" as well as the folder "features" exists. 在“ src”中,存在文件“ main.py”和“ init .py”以及文件夹“ features”。 In the folder "features" there are the files "featureExtraction.py" and " init .py". 在“功能”文件夹中,有文件“ featureExtraction.py”和“ init .py”。

In "main.py" I have the following import: 在“ main.py”中,我具有以下导入:

import src.features.featureExtraction

When I run main.py within pycharm everything works, but when I start the script on the command line using "python main.py" (beeing in the src folder), then I'm getting an error: 当我在pycharm中运行main.py时,一切正常,但是当我使用“ python main.py”(src文件夹中的beeing)在命令行中启动脚本时,出现了一个错误:

ModuleNotFoundError: No module named 'src'". ModuleNotFoundError:没有名为“ src”的模块。

Why does this happen? 为什么会这样?

You said you executed import src.features.featureExtraction while in the src folder. 您说您在src文件夹中执行了import src.features.featureExtraction That import makes sense only while you are in the parent MyProject folder. 仅当您在父MyProject文件夹中时,该导入才有意义。

In pycharm prefs, search for Project Structure and notice the content root (MyProject) that it is using. 在pycharm首选项中,搜索“项目结构”,并注意其正在使用的内容根目录(MyProject)。 That explains the different behavior you saw from IDE and from command line. 这就解释了您从IDE和命令行看到的不同行为。

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

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