How do you use pdflatex in Python? - python

I am trying to compile a .tex file in a Python script using pdflatex. The command pdflatex filename.tex works when I run it from the command line on Windows. However, attempting to run os.system("pdflatex filename.tex") just spits out 1 into the Python console and does not compile a pdf. I've also tried putting in the full file path similar to this person solved their problem but the same thing happens. Similarly, subprocess.call(['pdflatex', 'filename.tex']) just outputs a 1 and does not do anything.
It seems someone else has encountered the same problem in this thread, but on Mac instead of Windows. (But regardless of the operating system, they didn't find an answer.)
Why might this be happening?
EDIT: I've just discovered a solution. The script runs successfully (using the os.system approach) when I run the .py file using the command line. Previously I was attempting to run the script from RStudio, both using reticulate::source_python(filename) and also line-by-line via the reticulate REPL. Seems like the problem may actually be coming from R's reticulate package rather than anything to do with Python.
Fortunately RStudio has a terminal window so this doesn't end up being too inconvenient!

Related

QGIS - Cannot run executable .exe file or run matlab script through Python Console

i have matlab script which using few matlab libraries like Map ToolKit. I can run this script on MATLAB without any error. But i need to run this with QGIS and get output files.
Simply, script asks few question about Map Tools and get like .shp files and give output folder.
So to get these output files, i tried 3 ways:
I tried to run this code on Python Console. For that i installed matlab.engine and called script just like in documentation. But these matlab.engine doesn't let python to use matlab's own libraries so error on using shapeinfo function. It just let python to use few variables i think. So after failing on this i tried second way:
I created executable matlab file to run .exe file on python console. In here, there is a two way to get this exe file. First one is let you smaller file and doesn't include matlab runtime so you need to add matlab runtime on your path and i did it. Actually Matlab does that as default and i checked that it was okey. Executable file runs perfectly after clicking or on normal python scripts which is running on through CMD. But if i call Python Script in QGIS, it trying to open but returns Matlab runtime dll missing error. After i tried to compile with matlab runtime selection and results were same.
Lastly, i tried to run matlab.exe -r "try, run SCRPIT_PATH\script.m; end; quit on CMD and it works fine but if i call this on QGIS Python Console with os.system or subprocess.call functions, cmd appearing and closing immediately.
I guess QGIS doesn't let you to open some other applications somehow and i need to let it run. Or i just need to find import matlab libraries to python for work perfectly. I would be pleasure if you give any suggestion. Any solution for these ways or a different way would be great. Thanks.

python os.system("chcp 65001") does not work

Windows cmd does not handle utf8 well when executing print statements in python.
Many people said that we can type in "chcp 65001" before executing the python script in the cmd.
In our situation, we want to implement an app (in python pyc) with a desktop icon in Windows.
Thus our first solution is adding os.system("chcp 65001") to the source python script before the main function.
But somehow this did not work. Then some people said trying os.system("/k chcp 65001").
But this did not work either.
Our second soluiton is using a bat file with two lines, the first line is "chcp 65001" while the second is "python my.pyc" where my.pyc is main program of our app.
But this solution seems inconsistent since in some environment it failed while in others, it did not fail.
Moreover, with this solution, Windows does not allow us to change the logo (or icon image) of bat file.
Can someone help us solve this problem with cmd and utf8 with python scripts?

Problem running a R script from Python (Windows)

I've recently started programming in python for my job, so I'm quite new to it. My goal is to create a graphic interface so that the user can run a program that I have been developing in R. The interface is done using the Tkinter module from python (version 3.3).
The problem comes when I have to call the R interpreter from python to run an R file that is generated (run.R file). The curious thing is that this only happens when I try to run my script in Windows, not in Linux. In both cases, I am trying to use the os module from python.
​
This is the code that is not working for Windows:
os.chdir(outRW) #first I change the working directory to the one where the run.R file is
os.system("C:\R-3.6.1\bin\Rscript run.R")
When I execute this, it changes the directory successfully, but when it comes to calling the R interpreter, it shows me this error:
The filename, directory name, or volume label syntax is incorrect.
However, I have tried running the "C:\R-3.6.1\bin\Rscript run.R" command in the Windows Command Prompt and it works perfectly.
I have also tried adding the path to R to the environmental variables, but again I could only make it work in the Command Prompt, not with python.
I guess there is something very obvious that I am missing here, but I cannot see it.
Any help or comments are very much appreciated!
Thank you!
Use double backslashes.
In R you need to use double backslashes \\, otherwise it'll try to interpret it as an Escape Character.
Use this and it will work:
os.system("C:\\R-3.6.1\\bin\\Rscript run.R")
Hope this helps.

Script runs successfully on Spyder but not directly in Python

This a conceptual question. What reasons could motivate that a script runs successfully on Spyder IDE but not directly in Python. I am in Win7-64x. I think it is because Spyder is somehow more lenient.. maybe it uses hidden modules, libs, bats..? I found this problem when I was trying to make an executable using Pyinstaller, so I step back and check running the myscript.py on python directly like this on cmd,
python myscript.py
and it runs the script but throws normal errors like "list index out of range". The thing is they don't appear if executed inside the Spyder IDE, the script in Spyder is run fine without errors. I don't know the possible reasons. There is a way to roundabout that for pyinstaller, so that the produced executable runs fine like in Spyder? Suggestions? (Win7-64x, Python3.6, Spyder3.3.3)

git cannot execute python-script as hook

I have created a little pre-commit hook in python. This hook works like a charm under Linux, but in Windows it keeps telling me:
error: cannot spawn .git/hooks/pre-commit: No such file or directory
I know there have been similar questions here about the same issue and the conclusion seams to be the shebang. My script has this on the very first line:
#!F:\PortableApps\PortablePython3.2\App\python.exe
It's also interesting to note that executing the script simply by writing .git/hooks/pre-commit works wonderful, but as soon as I try to commit, git spits out the above message.
Another interesting thing is, when I convert the encoding from ANSI to UTF-8 (using Notepad++), I get the following error when trying to execute the script:
.git/hooks/pre-commit: Cannot execute binary file
I'm using the following tools:
PortablePython 3.2.1.1
msysgit 1.7.6 (Portable)
I used the proxy-approach to make the python script work under windows (with msysgit). The complete script (with description on how I did it) might be found here: https://gist.github.com/1839424
Here is the important part about making it work under Windows
If you're working with Windows (and "msysgit"), it's a little more complicated. Since "msysgit" seems to have a problem handling the SHEBANG, you'll have to use a little trick to make the script executable (further information on this problem can be found here).
In order to make the script work, you'll want to remove the SHEBANG from the Python script ("pre-commit.py") and use a wrapper bash-script to call the interpreter. This script should look something like this:
#!/bin/sh
python .git/hooks/pre-commit.py
Store this script as a file called "pre-commit" (no file-ending). This assumes that you have Python in your PATH. If you don't, you can also specify the full path to your interpreter-executable.
This script will be called by "git commit" and call the python-script to check for the huge files. The path after the SHEBANG should not be changed, as "msysgit" will remap it automatically. You must specify a path relative to the repo-root for the Python script to be executed (because thats from where the script is called).
Afterwards you'll want to copy both the wrapper-file ("pre-commit") and the Python-script ("pre-commit.py") to your repos ".git/hooks"-directory, personalize the Python-script ("max_file_size" and "git_binary_path") and mark the "pre-commit"-file executable.

Categories

Resources