简体   繁体   English

python3中正确的文件夹结构

[英]Proper folder structure in python3

Python 3.7.4 (homebrew) Python 3.7.4(自制软件)

I am trying to run script.py.我正在尝试运行 script.py。 It works fine in PyCharm when I run it, but when I try to run it from the command line I get an error.当我运行它时,它在 PyCharm 中运行良好,但是当我尝试从命令行运行它时,我收到一个错误。

Project structure:项目结构:

project
  /folder
    /subfolder1
      /subfolder2
        /script.py
  /tools
    /subfolder
      /tool.py

I get this error when I try to run it from command line当我尝试从命令行运行它时出现此错误

from tools.subfolder.tool import *
ModuleNotFoundError: No module named 'tools'

Using an editor like PyCharm will temporarily add the project root directory (in your case, directory project/ ) to the PYTHONPATH.使用像 PyCharm 这样的编辑器会将项目根目录(在您的情况下,目录project/ )临时添加到 PYTHONPATH。 This means that you have the possibility of importing modules by using a path relative to the project root.这意味着您可以使用相对于项目根目录的路径来导入模块。 That's why from tools.subfolder.tool import * works in PyCharm.这就是为什么from tools.subfolder.tool import *在 PyCharm 中有效。 If you wanted to run it from the command line, you could add your root directory to PYTHONPATH.如果您想从命令行运行它,您可以将您的根目录添加到 PYTHONPATH。 For example, if you're using bash or zsh , you can append your project root to PYTHONPATH by running export PYTHONPATH="$PYTHONPATH:/path/to/project/root/" .例如,如果您使用bashzsh ,则可以通过运行export PYTHONPATH="$PYTHONPATH:/path/to/project/root/"将项目根目录附加到 PYTHONPATH。 You can also do that for just the current script by running:您还可以通过运行以下命令仅针对当前脚本执行此操作:

import sys
sys.path.append('/path/to/project/root/')

You could also relatively specify the path to the project root.您也可以相对指定项目根目录的路径。 Say, your project root is in the directory holding the directory where your python script is located, you could do something like sys.path.append('..') instead.说,你的项目根目录在你的 python 脚本所在目录的目录中,你可以做类似sys.path.append('..')事情。

Cheers.干杯。

A similar question has been asked here: https://stackoverflow.com/questions/24622041/python-importing-a-module-from-a-parallel-directory . 在这里提出了类似的问题: https : //stackoverflow.com/questions/24622041/python-importing-a-module-from-a-parallel-directory Running a python script like yours from the terminal is a little bit different than just doing it normally, python subfolder1/subfolder2/script.py , because you have imports from outside the python script directory. 从终端运行像您这样的python脚本与正常运行python subfolder1/subfolder2/script.py有点不同,因为您是从python脚本目录外部导入的。

To do this you need to navigate into the root directory of your project with the cd command. 为此,您需要使用cd命令导航到项目的根目录。 In your case the folder called "Project" seems to be your project directory here. 在您的情况下,名为“ Project”的文件夹似乎是您的项目目录。 Secondly when you run the python script you should use python -m <python file directory> . 其次,当您运行python脚本时,应使用python -m <python file directory> For your project structure the command would be python -m folder.subfolder1.subfolder2.script 对于您的项目结构,命令为python -m folder.subfolder1.subfolder2.script

More information on this is in the link provided earlier. 有关更多信息,请参见前面提供的链接。 When you run a python script, for example, python subfolder1/subfolder2/script.py , it only adds subfolder2 to the PYTHONPATH so you can access files in the same directory, below it, but not above. 当您运行python脚本(例如python subfolder1/subfolder2/script.py ,它仅将subfolder2添加到PYTHONPATH中,因此您可以访问该目录下的同一目录中的文件,但不能访问该目录中的文件。 Running it this way, python -m folder.subfolder1.subfolder2.script , will add the project directory to the path making everything in it accessible. 以这种方式运行python -m folder.subfolder1.subfolder2.script ,会将项目目录添加到路径中,从而可以访问其中的所有内容。

I hope this helps with answering your question. 我希望这有助于回答您的问题。

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

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