我的项目如下所示:
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文件的内容如下所示:
main.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
,以root身份加载项目。然后,我右键单击并运行run_project.py文件。一切正常。我无法弄清楚如何以run_project.py
相同的方式从命令行运行。创建main.py
是在其他地方建议的解决方法,但是由于对模板文件的相对引用而失败(在实际项目中,该y
文件夹是git子模块,因此其中不能包含绝对引用)。
我回头圈了一下,并使其正常工作。请注意,我使用的是Windows 7,其中一些可能取决于平台。为了在不更改任何代码的情况下正常工作,关键是在运行之前设置PYTHONPATH环境变量。PyCharm会自动执行此操作(默认情况下,但是可以在“运行配置”选项中进行配置)。
脚步:
python run_project.py
Traceback (most recent call last): File "run_project.py", line 1, in <module> from c.z.the_module import the_module_function, the_module_write_function ImportError: No module named 'c'
SET PYTHONPATH=G:\TEST\running-pycharm-project-at-cmd-master
python run_project.py
Running run_project.py! the_module_function is running! The module module function is running! Here is support data: I am the support data!
START cmd /K "cd G:\TEST\running-pycharm-project-at-cmd-master\c & python run_project.py"
G:\TEST\myotherlocation run_it.bat
Running run_project.py! the_module_function is running! The module module function is running! Here is support data: I am the support data!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句