简体   繁体   English

从外部目录Python导入文件

[英]Importing a file from outside directory Python

I currently have a directory structure like so: 我目前有这样的目录结构:

- App
-- Model
-- Controller
-- View
-- main.py

I want to import main.py into a class into a controller file for testing purposes. 我想将main.py导入到控制器文件中的类中以进行测试。 How can I achieve this? 我该如何实现?

Add an empty __init__.py file in the directory App to turn that directory into a package . 在目录App添加一个空的__init__.py文件,以将该目录转换为

Then, provided the /path/to/App is visible in the sys.path of your python environment, you should be able to: 然后,假设/path/to/App在python环境的sys.path中可见,则您应该能够:

from App import main

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

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