简体   繁体   English

PyCharm 如何处理模块的相对导入?

[英]How does PyCharm handle relative imports of modules?

I have the following directory structure on a project that has been inherited:我在继承的项目上有以下目录结构:

work_dir/
    FlaskControlPanel/
        static/
        templates/
        app.py
    models/
        __init__.py
        model1.py
        model2.py
    script1.py
    script2.py

From script1.py or script2.py I can run this from models.model1 import Class1, Class2 and it works.script1.pyscript2.py我可以从script2.py from models.model1 import Class1, Class2运行它,它可以工作。 When using PyCharm, I can use the same import line within app.py .使用 PyCharm 时,我可以在app.py使用相同的导入行。

However, when I go to deploy this on my dev server, this doesn't work from app.py .但是,当我在我的开发服务器上部署它时,这在app.py I get the error ImportError: No module named models.model1我收到错误ImportError: No module named models.model1

What does PyCharm do to get around this? PyCharm 做了什么来解决这个问题?

How can I fix my import in app.py to work in both PyCharm and when I run my Flask application on the development server?当我在开发服务器上运行 Flask 应用程序时,如何修复我在app.py导入以在 PyCharm 中工作? I usually run it on the server just using python app.py from within the FlaskControlPanel directory.我通常只使用FlaskControlPanel目录中的python app.py在服务器上运行它。

You have to make sure work_dir is added in to sys.path or PYTHONPATH.您必须确保将work_dir添加到 sys.path 或 PYTHONPATH 中。

Python will add the parent folder of the script file into sys.path automatically. Python 会自动将脚本文件的父文件夹添加到 sys.path 中。 So if you run app.py in your shell, only FlaskControlPanel will be inserted into sys.path and python cannot find where models is ( work_dir is not in sys.path).因此,如果您在 shell 中运行 app.py,则只FlaskControlPanel插入到 sys.path 中,而 python 无法找到models所在的位置( work_dir不在 sys.path 中)。

But in pycharm, it will always add work_dir in sys.path.但是在 pycharm 中,它总是会在 sys.path 中添加work_dir That's why you can run app.py without any problem.这就是为什么您可以app.py运行app.py的原因。

For your problem, you can change directory to work_dir and run PYTHONPATH=. python FlaskControlPanel/app.py对于您的问题,您可以将目录更改为work_dir并运行PYTHONPATH=. python FlaskControlPanel/app.py PYTHONPATH=. python FlaskControlPanel/app.py

https://docs.python.org/2/using/cmdline.html#interface-options https://docs.python.org/2/using/cmdline.html#interface-options

Execute the Python code contained in script, which must be a filesystem path (absolute or relative) referring to either a Python file, a directory containing a __main__.py file, or a zipfile containing a __main__.py file.执行脚本中包含的 Python 代码,它必须是一个文件系统路径(绝对或相对),引用 Python 文件、包含 __main__.py 文件的目录或包含 __main__.py 文件的 zipfile。

If this option is given, the first element of sys.argv will be the script name as given on the command line.如果给出此选项,sys.argv 的第一个元素将是命令行中给出的脚本名称。

If the script name refers directly to a Python file, the directory containing that file is added to the start of sys.path, and the file is executed as the __main__ module.如果脚本名称直接引用 Python 文件,则将包含该文件的目录添加到 sys.path 的开头,并且该文件将作为 __main__ 模块执行。

If the script name refers to a directory or zipfile, the script name is added to the start of sys.path and the __main__.py file in that location is executed as the __main__ module.如果脚本名称引用目录或 zip 文件,则脚本名称将添加到 sys.path 的开头,并且该位置的 __main__.py 文件作为 __main__ 模块执行。

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

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