Linux Ubuntu how to start standard application scribus from python eclipse anaconda - python-3.x

I edited my question, hope it is described better now.
I am working on a software that gives me a nice PDF with lots of matplotlib graphics, depending on the data I get.
So think of a database of pages and then the software decides which pages are chosen and filled with changed images, The text stays the same.
So for instance for data1 I get page1-4 and page7 and page 9. For data2 I get page1-4 and page6. Saved as PDF. I am doing this manually with Quark which needs to be changed. I hope I can figure out the scripting to do so.
But for starters I cant start scribus from the developing enviroment. Eclipse Anaconda on Ubuntu.
import subprocess
subprocess.run('scribus')
works fine in terminal, but gives me an error in Eclipse which I cant figure out.
File "/home/b256/anaconda3/envs/test/lib/python3.7/site.py", line 178
file=sys.stderr)
^
SyntaxError: invalid syntax
This seems to be some Python 2 error in the site.py file
???? Is this some anaconda python path error ??

It's not really clear to me, what you want to achieve, but you're welcome to have a look at a script of mine:
https://github.com/aoloe/scribus-script-repository/blob/master/imposition/imposition.py
This is probably a bit more complex than what you are trying to achieve:
the script gets started from the terminal,
if it notices that it has not been started from inside of Scribus (the exception on import scribus)...
... it starts Scribus with itself as the Script to be run.
the script runs again, this time from inside of Scribus...
... now there is no exception when importing scribus and the body of the script runs.
Of course, it's simpler if you start a script that launches Scribus with other scripts.
For you the most important line is probably:
call(['scribus', '-g', '-py', sys.argv[0]] + arguments + ['--', file])
It's starting Scribus from Python
with as little GUI as possible (-g) and
launches the script sys.argv[0]
with a few arguments and
after the -- tells Scribus what file to open.

Related

Code work in PyCharm but doesn't work when lauching through python console

I have been working on a project and in the end it's suppose to print an output in a notepad file and it works perfectly in pycharm but as soon as I launch my file with python it crash every time I execute the code
I am 100% sure that the problem lies between the line I copied and pasted and I know that if start the project in pycharm with the python console setting active it work too so it may be some right problem? that's the conclusion I came with althought I'm not sure and I don't have any idea how to fix it even with my research. Ho and I,ve checked too and my python is up-to-date
number_of_product = [0,0,0,0]
Total = 0
with open("Bill.txt", "w+") as Bill:
Bill.write("{0} ChocoMilk\n".format(number_of_product[0]))
Bill.write("{0} Katkit\n".format(number_of_product[1]))
Bill.write("{0} N&N's\n".format(number_of_product[2]))
Bill.write("{0} SourJoes\n".format(number_of_product[3]))
Bill.write("Total : {0}$".format(Total))
The result should be 5 line of text written in a notepad file.
I found the problem: I was opening the Pycharm file by clicking "Open with" python3.7 and Windows wasn't giving me all the right I needed.
You need to change the basic application that runs the program from Pycharm to Python itself.

Do I need multiple run configurations - one per Python file - in Pycharm even though the only difference between them is the script?

I created a Python project in Pycharm which contains multiple Python files. As of just now, I need to create a run configuration for each Python file in my project, even though they're all the exact same - with the exception of the script.
This seems unnecessary and laborious and I would love to just use one run configuration for multiple Python files.
That said, I'm a novice Python programmer just getting started and so still unfamiliar with large parts of the language.
My Project Files:
My Run Configuration - Used for all Python files:
Some Research Carried Out
I've searched for a solution and explanation to this, but have been unable to find anything. Some of the places I've tried:
JetBrainsTV on youtube (https://www.youtube.com/watch?v=JLfd9LOdu_U)
JetBrains Website (https://www.jetbrains.com/help/pycharm/run-debug-configuration-python.html)
Stack Overflow
I hope there is sufficient detail here, if not I'd be happy to elaborate.
If those files are independent and you have nothing specific to them, then I see two simple ways of running them:
You don't have to manually create a run configuration for every file. You can just Right-Click on the file in the project tree and click "Run "
You can use the Terminal and run them files using the python interpreter as needed.
I was facing a similar situation when I started competitive programming. In my case I wanted to redirect my Test Cases from an input.txt file rather than manually typing the test cases for every run of my code. Using the above solution was not feasible, as I would need to manually change the Script Path and Redirect Input path in the Run Configuration window for every script I was running.
So what I wanted was, one run configuration, that would run all the scripts with Redirect Input path being set to input.txt.
To do that,
I created a main.py file with the following content:
import sys
if __name__ == '__main__':
fname = sys.argv[1]
exec(open(fname).read())
This main.py file is going to run my other python scripts.
Created this run configuration for the main.py file.
Now, every time I needed to run any code, with the code window open, ran this configuration, which actually executed main.py with current file name passed as its argument, which would then also take the inputs redirected from input.txt.
Hope this helps you or anyone trying to run multiple python scripts with a single run configuration in PyCharm.

How do I use output from SPARC solver as input to a python file?

My question: I need to save the output from a SPARC solver (which is currently appearing as text in the terminal) as a variable in my Python code. How can I do this?
Quick note: SPARC is a solver used for ASP (answer-set-programming) files - just mentioning this so that people don't get confused and think I am referring to asp.net.
I am running a Python file and an ASP file (in query mode) simultaneously, in the same terminal, using the command python pythonfile.py | java -jar sparc.jar aspfile.sp. Output from the python file in the form of sys.stdout.write() is being redirected as the input to the SPARC solver; i.e. the text I output becomes the query that is solved using my ASP code. This is working, and generating the output I want, but I can't figure out how to use that output in my Python code.
This is a follow-up to another question I have asked, found here. In that question I was trying to find out how to run an ASP file from my C++ code. I went with option 2 in the answer I was given, and am using redirected stdout with the two files running as separate processes. Please note one major change since the original question: I am now using Python instead of C++.
Further details if required: My Python version is 2.7 and my operating system is Ubuntu 14.04. I don't think it's relevant but, in case it is, you should know that my Python code is also being used to control a Gazebo Turtlebot simulation, and I am using ROS Indigo to run that simulation. I won't post my code unless someone requests it, as I just want an idea of what method I could use (I can't find anything that works on the internet), rather than needing my code debugged / assistance writing it. However I will post below an example of what is output to the terminal when my code runs, as this is the information I am trying to 'capture'.
SPARC V2.52
program translated
?- yes
?- no
It is the answers 'yes' and 'no' that I want to save as variables in my Python file.
SOLUTION:
For anyone wanting to do the same thing, I followed the answer provided by CaptainTrunky.
First I run the command python pythonfile.py | java -jar sparc.jar aspfile.sp > sparc.out, saving the SPARC output to the text file sparc.out.
Then I run python outputParser.py to run a script that prints the contents of the text file, allowing me to check that I'm manipulating the data correctly. The script is very simple:
lines = [line.strip('?- ') for line in open('sparc.out')]
lines = [line.strip('\n') for line in lines]
print lines
You can use python to read from sys.stdin with a script similar to the following (filter.py):
import sys
for line in sys.stdin.readlines():
if line.startswith('?- '):
print line.strip()
Then invoke your pipeline like this:
python pythonfile.py | java -jar sparc.jar aspfile.sp | python filter.py
I would suggest you do dump SPARK output to text file and then to parse it with you tool.
Write a shell script that does it for you:
python pythonfile.py | java -jar sparc.jar aspfile.sp > spark.out
python parse_out.py spark.out

How can I run my program without using PyCharm?

I have to finish the program on which I worked six months. This is my very first program.
I designed with PyCharm and I have a database and several module.
How can I run my program without using PyCharm?
Based on the fact that you are working on this project for the last 6 months, my guess is that you were running it through PyCharm. In most cases when you run your project through PyCharm you can see that on PyCharm "Run" console the first line logged is something like that:
/path/to/setup/bin/python<version> path/to/your_project/file_contains_main.py --argumnent1=value1 --argumnent2=value2...
Scenario 1
If you see something like that, then you can copy your project in a system that has python installed and run the same file_contains_main.py as below:
python path/to/your_project/file_contains_main.py --argumnent1=value1 --argumnent2=value2...
Some more details for Windows here and for Linux here.
Scenario 2
If you do not see the command above logged in PyCharm then you may find this solution more appropriate.

Python py2exe with tkinter (pyw file)

I have made a small program in Python that involves tkinter, and I made it a pyw file because it has a shortcut on the desktop and I do not want the command prompt to get in the way. I used py2exe following the sentdex tutorial on youtube, and I got an output file, but the output file shows an error and exits before I can read it. The pyw file on its own works fine, but I don't know how to get the exe output file to work correctly.
Information:
Python - 3.4.2;
OS - Windows 8.1;
Folder - Multiple items (photos and audio for the program);
Program - A simple animation in tkinter
If you need the program, tell me and I can upload the folder containing the program.
As far as I understand there is not a version of py2exe for python3.x
You'd be best of going for cx_freeze (Sentdex also has a tutorial on that on that)

Resources