0

I'm trying to launch may main Python script with some arguments listed in a txt file (config.txt).

Because parameters change almost every launch and I dont want to type them every time. They are not literally a "config" but I didn't find the correct file name (that's an other story).

See below:

-param1   1
-param2   2
-verbose

Using Run Configuration of PyCharm. Run/Debug PyCHarm Configuration

I would like to finally do something like : python C:\somewhere\main.py -param1 1 -param2 2 -verbose

Instead of current behavior :python C:\somewhere\main.py config.txt

Which, by the way, is missed understood by the program (obviously).

#32951846


I already tried windows for loops in the section "before launch: activate tools":

$: for /f "delims=" %x in (config.txt) do set ARGS=%ARGS%%x
$: python  C:\somewhere\main.py %ARGS%

But it only keep the last line of the config.txt inside ARGS.

#51948712


I also tried to pipe the content of the file into my python main program like:

python C:\somewhere\main.py < config.txt

But it do not work neither.

#syntax-redirection

Jérémy
  • 1,790
  • 1
  • 24
  • 40
  • If you pipe it in, you need to read the stdin. If you provide a file, you need to open and read the file. Neither option will be expanded to cli args, and I'm not sure that's possible, so what issues are you having parsing the file? – OneCricketeer Sep 25 '19 at 08:02
  • 1
    I do not want to give a file as parameter and to parse it, neither pip and read stdin; I just tried different solutions (that are not satisfying). The project is really big, I just simplified my problem here, to make it simple. Any tips to give file content as parameters would be welcome! – Jérémy Sep 25 '19 at 08:06
  • If anything, I think the for loop seems closest, but you'd then be passing a single argument to the script, not individual ones – OneCricketeer Sep 25 '19 at 08:10
  • And it only set the last line, because you're not accumulating ARGS as it's being built, you're just setting it to the value of x – OneCricketeer Sep 25 '19 at 08:11
  • I just corrected this miss copy/past. I did `for ... do ARGS=%ARGS%%x` and it only take the last one.. Notice that the `for` loop is supposed to fill the variable and after all, I use it for my script launch. But the result of the `for` loop is only the last line of the `config.txt` file. – Jérémy Sep 25 '19 at 08:12

2 Answers2

0

Am I right that you'd like to see something like https://youtrack.jetbrains.com/issue/PY-5543?

Consider using the following plugin: https://plugins.jetbrains.com/plugin/7861-envfile/

user2235698
  • 7,053
  • 1
  • 19
  • 27
  • I do not want to use environnement variables. Just to put a sequence of arguments like `main.py -arg1 a -arg2 b`. Where "-arg1 a -arg2 b" is as string inside a file. – Jérémy Sep 30 '19 at 14:52
0

This is not exactly what you were asking for, but you can follow this guideline to store the run configurations in a file and then modify the file, share it or add to git.

The key steps are to tick the box "Store as project file" in PyCharm's "Run/Debug Configurations" window. This will create the new subfolder "runConfigurations" in the ".idea" folder in the project folder.

The folder will contain an xml file with the line

<option name="PARAMETERS" value="&quot;arg1&quot; &quot;arg2&quot;" />

where "arg1" and "arg2" are the arguments which are passed to your script.

openwater
  • 23
  • 6