IPESE / REHO

Master version of the model
Apache License 2.0
6 stars 10 forks source link

[Bug]: <Relative Path in VScode> #13

Closed JosephLoustau closed 7 months ago

JosephLoustau commented 7 months ago

Is there an existing issue for this?

What happened?

It seems the relative path are not working as expected in VS code

Version

1.0.2 (Stable)

Relevant log output

No response

Anything else?

No response

JosephLoustau commented 7 months ago

There are two parts in that issue.

Giving a file path

Pycharm runs the script from the script itself while VScode does it from the opened directory by default. So using the function os.get_cwd() gives 2 different results:

So when later on the function os.path.realpath(path_given) is used, we obtain:

This can be corrected by changing the settings of VScode python.terminal.executeInFileDir. It can be done by:

:warning: This should be properly added to the documentation


Importing the module

Now, with the current directory set to the script, we are not able to import reho as it does not belong to the path.

The reason why it still works in Pycharm is that it automatically adds to the sys.path the opened directory and so the same should be done but manually in VScode.

The workaround is to add in the settings.json the path to the environment:

If you have not done it before, add in the settings the line:

"python.terminal.executeInFileDir": true

The final file should look like this:

{
    "python.defaultInterpreterPath": "user_path_to_python\\python3.10.exe",
    "[python]": {
        "editor.formatOnType": true
    },
    "git.openRepositoryInParentFolders": "always",
    "hediet.vscode-drawio.resizeImages": null,
    "hediet.vscode-drawio.codeLinkActivated": false,
    "hediet.vscode-drawio.theme": "atlas",
    "svg.preview.mode": "svg",
    "terminal.integrated.env.windows": {"PYTHONPATH": "${workspaceFolder}"},
    "python.terminal.executeInFileDir": true,
}
willu47 commented 4 months ago

Hi. I also have this same issue when working in the terminal. I have placed my .env file in a folder, and now wish to run the test run.py script which I downloaded from the repository using

    wget https://github.com/IPESE/REHO/raw/main/scripts/template/run.py

My folder looks like this:

my_folder
    - .env
    - run.py

Expected behaviour would be for the following to work:

$ cd my_folder
$ python run.py

But I also get the error involving .env.

DorsanL commented 2 months ago

Does the problem still remain after this commit https://github.com/IPESE/REHO/commit/4a63b03aec4f85ae097582e4bd1ab2c3b5d03e0c ?

willu47 commented 5 days ago

@DorsanL Yes, this problem remains. I thought it was due to the Cloud license in #34, but I believe it is this issue rearing its head again.

If I install REHO (e.g. pip install REHO), create a .env file in my local working folder and then run the test reho-test-import , I still get the error Exception: AMPL_PATH is not defined. Please include a .env file at the project root (e.g., AMPL_PATH='C:/AMPL'). Except now, I do not have a project root. I just have a working folder which could be anywhere on my file system. It seems that REHO is making assumptions about where the .env file should go, but this is incompatible with a pip install (which places the package in an arbitrary location on my computer).

DorsanL commented 4 days ago

This should no longer be a problem after the changes here: https://github.com/IPESE/REHO/commit/b0ba53c1118e49b7a52d66655314b7280491da82

To sum up: