gpt4 book ai didi

python - 我如何复制 PyCharm 在命令行运行我的 Python 3.4 项目的方式?

转载 作者:太空狗 更新时间:2023-10-29 20:31:20 27 4
gpt4 key购买 nike

我的项目是这样的:

running-pycharm-project-at-cmd
- main.py
- c
- run_project.py
- z
- __init__.py
- the_module.py
- y
- __init__.py
- template.md
- the_module_module.py
- the_support_function.py

.py 文件的内容如下所示:

主.py

from c.run_project import run

print('running main.py...')
run()

c/run_project.py

from c.z.the_module import the_module_function, the_module_write_function

def run():
print('Running run_project.py!')
the_module_function()
# write a file:
the_module_write_function(read_file='./z/y/template.md', write_file='../README.md')

if __name__ == '__main__':
run()

c/z/the_module.py

from c.z.y.the_module_module import the_module_module_function

def the_module_function():
print('the_module_function is running!')
the_module_module_function()
pass

def the_module_write_function(read_file, write_file):
with open(read_file, 'r') as fid:
with open(write_file, 'w') as fid_out:
contents = fid.read()
contents.replace('{}', 'THE-FINAL-PRODUCT!')
fid_out.write(contents)

c/z/y/the_module_module.py

from .the_support_function import this_support_data

def the_module_module_function():
print('The module module function is running!')
print("Here is support data: {}".format(this_support_data))
pass

c/z/y/the_support_function.py

this_support_data = "I am the support data!"

GitHub 存储库使复制更容易:running-pycharm-project-at-cmd

问题:在 Pycharm 中以 running-pycharm-project-at-cmd 作为根加载项目。然后我右键单击并运行 run_project.py 文件。一切正常。我不知道如何以相同的方式从命令行运行 run_project.py。创建 main.pya workaround suggested elsewhere , 但由于对模板文件的相对引用而失败(在实际项目中, y 文件夹是一个git子模块,因此不能在其中有绝对引用)。

最佳答案

如果您将 main.py 文件复制到 c 文件夹并将其重命名为 __init__.py,那么您可以运行:

$ python -m c

-m 参数告诉 python 运行模块或包(在本例中为 c)。 Python 将在 c 的文件夹中查找 __init__.py 文件并运行它。您只需要确保文件夹 c 在您的 PYTHONPATH 中或者是当前工作目录的子文件夹。

关于python - 我如何复制 PyCharm 在命令行运行我的 Python 3.4 项目的方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33740007/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com