I'm trying to run some code on VSCode, as I'm shifting over from PyCharm and Vim to utilise VSCode as a general purpose editor for a variety of programming languages.
I'm currently trying to make some code work, which should have been quite straight forward given it is a project in my PyCharm IDE and I have been able to run it there.
When I open up my VScode workspace, and try and run the code, it cannot find a directory that obviouslly exists:
So I'm getting this error:
Exception has occurred: FileNotFoundError data/connectivity/weights.txt not found.
And, my code is also pretty straight forward, its something like this:
SC = np.loadtxt(join("data/connectivity", "weights.txt"))
numpy is loaded in as np. and os.path.join is loaded in as join. The directory data/ is in the same parent directory as the code/script I'm running.
I have no idea why it isn't recognising the path, or the folder.
**NOTE: ** To add, when I run my code in jupyter notebook in VSCode, it runs smoothly and recognises the path. The jupyter notebook is also in the same parent directory as the data/ directory. So there is no visible difference between the .py and .ipynb files regarding code and relative position to the data/ directory.
Any help would be much appreciated. There is probably a simple solution that I'm failing to catch onto at the moment.
Thanks, Boki
I've tried changing the directory to an absolute path on my Mac, which didn't work.
I also tried--though probably not so usefully--adding __init__py files to each of the subdirectories, but as they are not modules, this was sort of pointless, I was just trying to be exhaustive.
As I mention above, code works perfectly when I run the same code as a .ipynb, or when I run it in my old IDE: PyCharm
CodePudding user response:
This is because the current directory of the terminal is different when vscode and pycharm run files.
By default, vscode executes files in the workspace, and pycharm executes files in the folder where the files are run.
You can search Terminal: Execute In File Dir in settings and check it.
This will be consistent with pycharm.
Of course, the default setting of vscode is also beneficial. When encountering multi-level directories, it is more convenient to import files.
CodePudding user response:
sounds like a sandbox issue. Does vscode have access to the entire disk? How about bash or whatever process vscode is running notebooks under? It could also be the python version you picked which needs access.