Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 739
skulz00 Avatar asked Nov 16 '15 16:11

skulz00


People also ask

How do I start PyCharm from a directory?

To run PyCharm, find it in the Windows Start menu or use the desktop shortcut. You can also run the launcher batch script or executable in the installation directory under bin. Run the PyCharm app from the Applications directory, Launchpad, or Spotlight.


1 Answers

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

$ python -m c

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.

like image 121
Brad Campbell Avatar answered Sep 17 '22 12:09

Brad Campbell