Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
JosephLoustau opened this issue Mar 6, 2024 · 5 comments
Closed
1 task done

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

JosephLoustau opened this issue Mar 6, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@JosephLoustau
Copy link
Collaborator

Is there an existing issue for this?

  • I have searched the existing issues

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 JosephLoustau added the bug Something isn't working label Mar 6, 2024
@JosephLoustau
Copy link
Collaborator Author

JosephLoustau commented Mar 11, 2024

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:

  • Pycharm : user_path_to_reho\REHO_tools\REHO\scripts\examples

  • VScode : user_path_to_reho\REHO_tools\REHO\

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

  • Pycharm : user_path_to_reho\REHO_tools\REHO\scripts\template\data\buildings.csv

  • VScode : user_path_to_reho\REHO_tools\template\data\buildings.csv

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

  • Going into File > Preferences > Settings > Python > Terminal: Execute In File Dir
  • Setting the "python.terminal.executeInFileDir": true in the settings.json file.

⚠️ 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:

  • Windows : Ctrl + Shift + P > open settings > click on Preferences: Open User Settings (JSON) > write:
"terminal.integrated.env.windows": {"PYTHONPATH": "${workspaceFolder}"}
  • Mac : F1 > open settings > click on Preferences: Open User Settings (JSON) > write:
"terminal.integrated.env.osx": {"PYTHONPATH": "${workspaceFolder}"}

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
Copy link

willu47 commented May 20, 2024

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
Copy link
Collaborator

DorsanL commented Jul 23, 2024

Does the problem still remain after this commit 4a63b03 ?

@willu47
Copy link

willu47 commented Oct 10, 2024

@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
Copy link
Collaborator

DorsanL commented Oct 11, 2024

This should no longer be a problem after the changes here: b0ba53c

To sum up:

  • an .env file is no longer required with the cloud-based Community Edition license
  • to use a standard local license, one must specify the AMPL_PATH environment variable in a .env file that can be located either at the package root directory (i.e., in the reho/ directory) or at the current working directory (i.e., where the script is run)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants