cannot run program from terminal using pycharm 2017

Answered

 

I often run a program using terminal because that speeds things up by a factor of 3. I recently upgraded from a 2016 version of Pycharm to a 2017 version and now I can no longer run the program in terminal. The line that the terminal does not like is:

wb4 = load_workbook('../temp_proof.xlsx')

Again, this line works fine with Pycharm. I went to the terminal and made sure it was in the right directory and when I pressed ls, that file is in the directory.

Here is the terminal error message:
 File "/Users/bobsmith/PycharmProjects/inference_engine2/inference2/Proofs/prove.py", line 36, in <module>
    wb4 = load_workbook('../temp_proof.xlsx')

  File "/Library/Python/2.7/site-packages/openpyxl/reader/excel.py", line 151, in load_workbook
    archive = _validate_archive(filename)

  File "/Library/Python/2.7/site-packages/openpyxl/reader/excel.py", line 115, in _validate_archive
    archive = ZipFile(filename, 'r', ZIP_DEFLATED)

  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 756, in __init__
    self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: '../temp_proof.xlsx'
0
6 comments

I have also tried using the full path of the document

 

0

Hi Kyle! When you execute a script in PyCharm it will set the script folder as a working directory. In your case it will be

/Users/bobsmith/PycharmProjects/inference_engine2/inference2/Proofs/

you can check it inside Run Configuration to be sure. Then assuming

temp_proof.xlsx

is inside

/Users/bobsmith/PycharmProjects/inference_engine2/inference2/

the reference

../temp_proof.xlsx

is correct indeed. Meanwhile if you open the console PyCharm will set a Project root as a working directory, in your case it will be

/Users/bobsmith/PycharmProjects/inference_engine2/

so the previous path to xlsx file will become obviously not correct. The absolute system path should work here but it's hard to tell what exactly is wrong without looking in your code and path provided. I would use

print(os.getcwd())

to get the working directory in runtime and debug the problem. If you are allowed to share the project or a related part of it - please contact our support with additional details pycharm-support@jetbrains.com. Feel free to comment here also but this thread is not private.

0

I tried changing my working directory by using

Users/kylefoley/PycharmProjects/inference_engine2

but that did not work.  Incidentally the temp_proofs doc is in directory

Users/kylefoley/PycharmProjects/inference_engine2

 

so when I move it into the same folder as the script and change the line to

 

wb4 = load_workbook('Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/temp_proof.xlsx')

I get the same error message which is

File "/Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/prove.py", line 36, in <module>

    wb4 = load_workbook('Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/temp_proof.xlsx')

  File "/Library/Python/2.7/site-packages/openpyxl/reader/excel.py", line 151, in load_workbook

    archive = _validate_archive(filename)

  File "/Library/Python/2.7/site-packages/openpyxl/reader/excel.py", line 115, in _validate_archive

    archive = ZipFile(filename, 'r', ZIP_DEFLATED)

  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 756, in __init__

    self.fp = open(file, modeDict[mode])

IOError: [Errno 2] No such file or directory: 'Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/temp_proof.xlsx'

 

0

Kyle, looks like you forgot an initial slash in the path:

wb4 = load_workbook('Users/kylefoley/PycharmProjects/...

should be

wb4 = load_workbook('/Users/kylefoley/PycharmProjects/...

 

0

Thanks, that did it.

 

Also by the way, the syntax

 

print(os.getcwd())

NameError: name 'os' is not defined

 

did not work.

0

Oh yeap, you have to import os module first:

import os

print(os.getcwd())
0

Please sign in to leave a comment.