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

sku*_*z00 9 python command-line project-structure pycharm python-3.x

我的项目看起来像这样:

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
Run Code Online (Sandbox Code Playgroud)

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

main.py

from c.run_project import run

print('running main.py...')
run()
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

C/Z/Y/the_support_function.py

this_support_data = "I am the support data!"
Run Code Online (Sandbox Code Playgroud)

GitHub repo使复制更容易:running-pycharm-project-at-cmd

问题:在Pycharm running-pycharm-project-at-cmd中以root 身份加载项目.然后我右键单击并运行run_project.py文件.一切正常.我无法弄清楚如何以run_project.py相同的方式从命令行运行.创建main.py其他地方建议的变通方法,但由于对模板文件的相对引用而失败(在实际项目中,该y文件夹是一个git子模块,因此不能在其中包含绝对引用).

sku*_*z00 0

我又回到了这个问题,并且能够让它工作。请注意,我使用的是 Windows 7,其中一些可能取决于平台。为了在不更改任何代码的情况下使其正常工作,关键是在运行之前设置 PYTHONPATH 环境变量。PyCharm 自动执行此操作(默认情况下,但可以在“运行配置”选项中进行配置)。

脚步:

重现问题:

  • 将 github 存储库克隆到 G:\TEST。
  • 导航到 G:\TEST\running-pycharm-project-at-cmd-master\c
  • 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'

最简单的解决方案(如果能够从脚本位置运行Python):

  • 运行前设置 PYTHONPATH 环境变量。例如

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!

从远程位置运行(这绝对是 Windows 特定的):

  • 创建一个 .bat 文件,在运行 Python 之前导航到正确的工作目录。IE:

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!