multiple virtual env usage for different directories inside single workspace - visual-studio-code

I have a project in which I use multiple python virtual environments. For each directory, I use one different virtual environment. Is it possible to configure this in order to not change environment manually each time I need to execute files in another dir?
Just to be more clear, if my workspace is like this:
dir1
file1.py
file2.py
dir2
file3.py
file4.py
I would like to link dir1 with virtual env venv1 and dir2 with venv2. This way, whenever I run file1.py or file2.py, it would automatically use venv1, and if I run file3.py or file4.py, it should use venv2.
I'm checking this link and my first thought is configuring it with a debugging launch file via the 'python' argument. The problem with this is that I should create multiple launch options and execute each python file in debug mode.
Is there any other way? Like using workspace setttings (json file) but for each subdirectory I have? Or maybe using the settings of the workspace with a custom variable that is changed based on the directory where I execute the python file?

Related

Eclipse Path Variable to Output folder

I need to start an external tool in eclipse, which starts with some arguments. One of them revers to the output file. My problem is, that I have more than one build configuration, so I have multiple output folders, but I want to use just one external tool config.
External tool config
Is there A way, where I can set the output folder as a dynamic variable, which depends on the selected build config.
There exists a variable config_name:
https://www.eclipse.org/forums/index.php/t/1076833/
${config_name} of the current project is given by:
${config_name:${ProjName}}

How to run `forest schema:update` outside project directory?

I'm trying to use the forest-cli schema:update command, but when I do, I keep getting the error:
× We are not able to detect a Forest CLI project file architecture at this path: /PATH/TO/REPO/ROOT.: Error: No "routes" directory.
There is a routes directory, but within src/ below the repo root. I have tried running forest schema:update from inside there, but I get the exact same error. The command only has options for a config file and an output directory.
Googling has turned up nothing, and there's no obvious hint from forestadmin's documents. Thanks in advance for any assistance!
According to the forest-cli code available here, the forest schema:update command requires the package.json file to be directly accessible in order to run (In the same folder you run the command), to check that the version of the agent you are running is indeed compatible with schema:update.
You can also use the -c/--config option in order to use another location of your config/database.js, and the -o/--outputDirectory to output the result to a new location.
In your case, I would say that forest schema:update -c src/config/database.config.js -o tmp should allow you to generate the files in the tmp directory (Be aware that this directory should not exist).
This command should be run where your package.json is located.
However, I don't think you will be able to export files directly at the right location when using a custom folder structure.

VSCode: how to structure a simple python package with few modules and tests, debugging and linting?

I'm having more trouble than I'd like to admit to structure a simple project in Python to develop using Visual Studio Code.
How should I structure in my file system a project that is a simple Python package with a few modules? Just a bunch of *.py files together. My requisites are:
I must be able to step debug it in vscode.
It has a bunch of unit tests using pytest.
I can select to debug a specific test from vscode tab and it must stop in breakpoints.
pylint must not show any false positives.
The test files must be in a different directory of the main module files.
I must be able to run all the tests from the console.
The module is executed inside a virtual environment using python standard lib module venv
The code will use type hints
I may use another linter, even another test framework.
Nothing fancy, but I'm really having trouble to get it right. I want to know:
How should I organize my subdirectory: a folder with the main files and a sibling folder with the tests? Or a subfolder with the code and a subsubfolder with the tests?
Which dirs must have a init.py file?
How the tests should import the files from the module? Should I use relative imports?
Should I create a pytest.ini file?
Should I create a .env file?
What's the content of my launch.json the debugger file config in vscode?
Common dir structure:
app
__init__.py
yourappcode.py
tests (pytest looks for this)
__init__.py
test_yourunittests.py
server.py if you have one
.env
.coveragerc
README.md
Pipfile
.gitignore
pyproject.toml if you want
.vscode (helpful)
launch.json
settings.json
Or you could do one better. Ignore my structure and look at the some of famous python projects github page. Like fastAPI, Flask, asgi, aiohttp are some that I can think of right now
Also:
I think absolute imports are easier to work with compared to relative imports, I could be wrong though
vscode is able to use pytest. Make sure you have a testing extension. Vscode has a built in one im pretty sure. You can configure it to pytest and specify your test dir. You can also run your test from command line. If youre at the root, just running ‘pytest’ will recognise your tests dir if it’s named that by default. Also your actual test files need to start with prefix test_ i think.
The launch.json doesn’t need to be anything special. When you click on the settings button next to play button in the debug panel. Vscode will ask what kind of app is it. I.e If its a flask app, select python then select flask and it will auto generate a settings file which you can tweak however you want in order to get your app to run. I.e maybe you want to expose a different port or the commands to run your app are different
It sounds to me like you just need to spend a bit of time configuring vscode to your specific python needs. For example, you can use a virtualenv and linting in whichever way you want. You just need to have a settings.json file in the .vscode folder in your repo where you specify your settings. Configurations to specify python virtualenv and linting methods can be found online

How do I run a project in eclipse with different jboss-ejb-client.properties

I have EJBs deployed on several different servers, for different environments. I have many projects that use these EJBs. I usually just run my projects against the DEV server EJBs, but sometimes I need to run against the TEST or PROD environment EJBs. This necessitates having to comment out all of the DEV nodes in my jboss-client-ejb.properties file and uncomment all of the TEST nodes. But then if I forget to change them back, I may mess up some data if I run it later. What I would like to do is create a different runtime configuration for each environment, and have each runtime config use a different version of the jboss-client-ejb.properties. Is there a way to do this? If so how? I have looked at all of properties of a run configuration, and don't see anything helpful.
In eclipse preferences search for string variable substitution. Here create variables that point to multiple config files for each of your environments. Then create multiple run configurations and for each one (like dev or prod) add a program argument that points to your string variable defined in your preferences like this -DmyconfigFile={$MyDevPropertiesFilePath}, or you could hard code the config path and have multiple runtime configurations that use different config files. Key point here is create multiple runtime launch configurations for each environment and add the properties for each environment that point to the config file respective to each environment. This way you can easily select the launch menu and decide to run "dev" "prod" or whatever you name your multiple configurations. Trying to do this with one runtime configuration will cause pain as you say, because it is easy to forget to revert or change the config file you want to to use. Hope that helps. Also if you create a new workspace you can export your runtime configurations using the export wizard which is also helpful for passing on to other developers or putting in source control.
P.S Looking more at your question you wan to pass in the config file path as a program argument, you are correct there are no specific options for setting this file path. Using program arguments with multiple launch configurations.

Eclipse Erlide how to choose default current directory

I have installed Erlide in Eclipse, and trying to create an application. The Erlang project is named demo. It contains three folders - ebin, include and src. The demo.erl file is in src, for simply printing Hello World.
To run it in Eclipse shell, I choose Run from the top menu, and click Run as Erlang Application. Then a console opens in the sidebar. There I type c(demo) and press CTRL-Enter. Then I get an error message like demo.erl:none: no such file or directory.
Then I do pwd(), and it shows C:/Users/myName/Desktop/eclipse-jee-juno-SR1-win32-x86_64/eclipse even though the Erlang project is in workspace C:/Users/myName/Desktop/workspace/Erlang. Is there any way make Erlide run all Erlang applications from their directory directly?
If you have set Project -> Build Automatically in Eclipse to true, you don't need to type c(demo). explicitly to compile the demo module, it's done automatically when you save your file. But if you have not set it, you'll get exception error: undefined function if you try to use some of the functions in the module for example.
To make the c(demo) command work properly and find your .erl files, there are several ways to set the current working directory:
Use Run -> Run Configuration -> Erlang Application -> Runtimes -> Working directory -> enter the full path to the /src folder of your project (without quotes).
Then hit Apply and restart Eclipse (File -> Restart). If you try to Run the project without first restarting the IDE, your changes will not take effect i.e. the pwd(). command will still be returning the old working directory path.
You can set the working directory in the erlide console with the command c:cd(the/full/path/to/your/src/folder)., before the call to c(demo). The path should be put in quotes.
You can also set the working directory in the .erlang file by typing there the same command c:cd(the/full/path/to/your/src/folder). (the path again should be put in quotes). The file must be placed in you user directory, for Windows this would be C:/Documents and Settings/YourUsername. It's the same directory were also .erlang.cookie and erlide-debug.txt are placed. Windows Explorer will not allow you to create a file named .erlang, so you have to use the command copy NUL .erlang in Command Prompt (cmd.exe) for this. Then you can edit it with any txt redactor. Don't forget to restart Eclipse again after it.
Also note the following:
You can use only / and not \ in the path (even in Windows).
If you have entered a wrong path (with \ or to a non existing directory), it will be ignored and the working directory will not be changed at all.
After changing the working directory to your/src folder, your beam files will start to appear there too instead of the /ebin folder.
If you set a different path in both .erlang file and Run -> Run Configuration -> Erlang Application -> Runtimes -> Working directory, pwd(). will return the path in .erlang file.
You don't need to run c(demo). The beam code is loaded and reloaded automatically, whenever the source is changed and saved.
If you still need to set a working directory, go to run->run configuration and you can edit the configuration.
/Vlad