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

时间:2022-03-28 20:23:59

My project looks like this:

我的项目是这样的:

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

The contents of the .py files are shown below:

.py文件的内容如下:

main.py

main.py

from c.run_project import run

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

c/run_project.py

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

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

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

c / z / y / the_support_function.py

this_support_data = "I am the support data!"

GitHub repo to make replication easier: running-pycharm-project-at-cmd

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

The problem: in Pycharm load up the project with running-pycharm-project-at-cmd as the root. Then I right click and run the run_project.py file. Everything works fine. I cannot figure out how to run run_project.py from the command line in the same way. Creating main.py was a workaround suggested elsewhere, but it fails due to the relative references to the template file (in actual project, the y folder is a git submodule and therefore cannot have absolute references in it).

问题:在Pycharm中,以运行- Pycharm -project-at-cmd作为根节点加载项目。然后右键单击并运行run_project。py文件。一切工作正常。我不知道如何运行run_project。py以同样的方式从命令行发出。创造主。py是其他地方建议的一种变通方法,但是由于模板文件的相对引用而失败(在实际项目中,y文件夹是一个git子模块,因此不能在其中包含绝对引用)。

2 个解决方案

#1


1  

If you copy your main.py file into the c folder and rename it __init__.py, then you could run:

如果你复制你的主页。将py文件放入c文件夹并将其重命名为__init__。py,你可以跑了:

$ python -m c

python c - m美元

The -m argument tells python to run a module or package (in this case c). Python will look in the c's folder for an __init__.py file and run that. You just need to ensure that the folder c is either in your PYTHONPATH or is a subfolder of the current working directory.

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

#2


0  

I circled back to this and was able to get it working. Note that I am on Windows 7, and some of this might be platform dependent. In order to get this working without changing any code, the key is setting PYTHONPATH environment variable before running. PyCharm does this automatically (by default, but it is configurable in the Run Configuration options).

我绕了一圈,终于让它工作了。注意,我使用的是Windows 7,其中一些可能与平台有关。为了在不改变任何代码的情况下工作,关键是在运行之前设置PYTHONPATH环境变量。PyCharm会自动执行这个操作(默认情况下,但是在运行配置选项中是可配置的)。

Steps:

步骤:

Recreating the issue:

  • Clone the github repo to G:\TEST.
  • 克隆github的repo到G:\测试。
  • Navigate to G:\TEST\running-pycharm-project-at-cmd-master\c
  • 导航到G:\ \ running-pycharm-project-at-cmd-master \ c进行测试
  • python run_project.py
  • 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'

回溯(最近一次调用):文件“run_project”。py",第1行, <模块> 来自c.z。the_module导入the_module_function, the_module_write_function ImportError:没有名为“c”的模块

Simplest Solution (if able to run Python from the script location):

  • set the PYTHONPATH environment variable before running. e.g.
  • 在运行之前设置PYTHONPATH环境变量。如。

SET PYTHONPATH=G:\TEST\running-pycharm-project-at-cmd-master

设置到PYTHONPATH = G:\ \ running-pycharm-project-at-cmd-master测试

  • Now python run_project.py
  • 现在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!

运行run_project.py !the_module_function运行!模块模块功能正在运行!这里是支持数据:我是支持数据!

To run from a remote location (this is definitely Windows specific):

  • Create a .bat file that navigates to correct working directory before running Python. i.e.:
  • 创建一个.bat文件,在运行Python之前导航到正确的工作目录。例如:

START cmd /K "cd G:\TEST\running-pycharm-project-at-cmd-master\c & python run_project.py"

启动cmd /K "cd G:\测试\ -pycharm-project-at-cmd-master\c & python run_project.py"

G:\TEST\myotherlocation run_it.bat

G:\测试\ 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!

运行run_project.py !the_module_function运行!模块模块功能正在运行!这里是支持数据:我是支持数据!

#1


1  

If you copy your main.py file into the c folder and rename it __init__.py, then you could run:

如果你复制你的主页。将py文件放入c文件夹并将其重命名为__init__。py,你可以跑了:

$ python -m c

python c - m美元

The -m argument tells python to run a module or package (in this case c). Python will look in the c's folder for an __init__.py file and run that. You just need to ensure that the folder c is either in your PYTHONPATH or is a subfolder of the current working directory.

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

#2


0  

I circled back to this and was able to get it working. Note that I am on Windows 7, and some of this might be platform dependent. In order to get this working without changing any code, the key is setting PYTHONPATH environment variable before running. PyCharm does this automatically (by default, but it is configurable in the Run Configuration options).

我绕了一圈,终于让它工作了。注意,我使用的是Windows 7,其中一些可能与平台有关。为了在不改变任何代码的情况下工作,关键是在运行之前设置PYTHONPATH环境变量。PyCharm会自动执行这个操作(默认情况下,但是在运行配置选项中是可配置的)。

Steps:

步骤:

Recreating the issue:

  • Clone the github repo to G:\TEST.
  • 克隆github的repo到G:\测试。
  • Navigate to G:\TEST\running-pycharm-project-at-cmd-master\c
  • 导航到G:\ \ running-pycharm-project-at-cmd-master \ c进行测试
  • python run_project.py
  • 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'

回溯(最近一次调用):文件“run_project”。py",第1行, <模块> 来自c.z。the_module导入the_module_function, the_module_write_function ImportError:没有名为“c”的模块

Simplest Solution (if able to run Python from the script location):

  • set the PYTHONPATH environment variable before running. e.g.
  • 在运行之前设置PYTHONPATH环境变量。如。

SET PYTHONPATH=G:\TEST\running-pycharm-project-at-cmd-master

设置到PYTHONPATH = G:\ \ running-pycharm-project-at-cmd-master测试

  • Now python run_project.py
  • 现在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!

运行run_project.py !the_module_function运行!模块模块功能正在运行!这里是支持数据:我是支持数据!

To run from a remote location (this is definitely Windows specific):

  • Create a .bat file that navigates to correct working directory before running Python. i.e.:
  • 创建一个.bat文件,在运行Python之前导航到正确的工作目录。例如:

START cmd /K "cd G:\TEST\running-pycharm-project-at-cmd-master\c & python run_project.py"

启动cmd /K "cd G:\测试\ -pycharm-project-at-cmd-master\c & python run_project.py"

G:\TEST\myotherlocation run_it.bat

G:\测试\ 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!

运行run_project.py !the_module_function运行!模块模块功能正在运行!这里是支持数据:我是支持数据!