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

2

9

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

Labionasal answered 16/11, 2015 at 16:23 Comment(0)
L
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).

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!

Labionasal answered 9/6, 2016 at 17:56 Comment(0)
B
1

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.

Basket answered 16/11, 2015 at 18:41 Comment(2)
Question may have been unclear. I want to maintain PyCharm method of running and be able to run in command line. I tried your change. In PyCharm it executes twice. In command line, I get the same issue as using the main.py approach: FileNotFoundError: [Errno 2] No such file or directory: './z/y/template.md'. I ran the python command from the root folder: python -m c (so I think that meets your second condition: subfolder of current working directory)Labionasal
Within a python file you can use automatic variable __file__ to get the path of the python file. You can use os.path.dirname to get the directory that the python script is in. Use this as the basis for any relative paths for 'program data'. I do this quite often.Basket
L
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).

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!

Labionasal answered 9/6, 2016 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.