Search code examples
pythonpython-3.xcommand-linepycharmproject-structure

How do I replicate the way PyCharm is running my Python 3.4 project at the command line?


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:

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 repo to make replication easier: running-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).


Solution

  • 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).

    Steps:

    Recreating the issue:

    • Clone the github repo to G:\TEST.
    • Navigate to 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'

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

    • set the PYTHONPATH environment variable before running. e.g.

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

    • Now 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!

    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.:

    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!