About VSC, it doesn't work well(Python, windows) - python

When I run python code in the VSC, there's a problem.
When I initially run shift+enter to run each code in a particular line, it works well. However, after then that, when I try to run the overall code by using triangle button, there's a problem.
Same as the above, when I initially run the overall code by using triangle button, then I can't run each of code in one line with shift+enter
ex.1) When I use loop code like 'for i in range~..', the loop code works well. But when trying to do it by running the overall code, it doesn't work(specially, terminal shows that there's an indentation error.
2x.2) When I call some files on my C drive, I can call it by using shift+enter to run each code. And when I try to do it by using triangle code to make it, it doesn't work with error(invalid syntax).
(result with using shift+enter to call files)
import os
file_path = os.path.join('enron1/ham/0007.1999-12-14.farmer.ham.txt')
(result with running overall codes)
& C:/Users/gram/AppData/Local/Microsoft/WindowsApps/python3.10.exe c:/Users/gram/Desktop/강의/기계학습/Practice/test.py
File "<stdin>", line 1
& C:/Users/gram/AppData/Local/Microsoft/WindowsApps/python3.10.exe c:/Users/gram/Desktop/강의/기계학습/Practice/test.py
^
SyntaxError: invalid syntax
(reference)
I re-installed VSC and other python files(Anaconda3) to fix this problem, it was useless.

File "", line 1
first you need to get out from the this triangle line(>>>) by write >>>ctrl+z
then you can run your entire code .

Related

FileNotFound Error in python when running .py file from visual basic

I was trying to make a assistant which can perform simple task like shutting down the computer etc. For this i chose python and visual basic..... visual basic for displaying(frontend applicaton) and python for performing tasks(backend application). So i create a py file named main.py and created a folder named query and in it created a file named query.jarvis which can simply be opened as a text file. The vb(visual basic) program just write text into query.jarvis and then run the main.py file. When I run it manually by double clicking the main.py file it works fine(like in query was "shutdown" and after running main.py file by double clicking my computer shutdown) but when I try to run it from vb it shows the error file not found query\query.jarvis . I even tried to convert py file to exe by pyinstaller but it again showed the same error but only when I run it from vb.
*main.py()
def check(q):
#here was performing task according to query
f=open("query\query.jarvis")
#here the error occured
x=f.readlines()
d=x[0]
d=d.strip()
q=d.lower()
check(q)*
*vb.net
objWriter123.Close()
Dim objWriter As New System.IO.StreamWriter(moddir + "query\query.jarvis")
#here moddir is the directory of main.py file
objWriter.Write(UserQuery.Text)
objWriter.Close()
UserQuery.Text = ""
Process.Start(moddir + "main.py", AppWinStyle.MinimizedNoFocus)*
process.start sometimes has weird outcomes. i usually manage to fix it by adding explorer.exe into the mix.
Process.Start("explorer.exe", moddir & "\main.py")
also note this extra backslash, that you might have missed. in \main.py
note
in vb you concat strings with the & symbol instead of the + symbol.

Executing R files from inside Python using PypeR

My current work project is on writing a Python program that must at various points rely on R. Since I don't know R so well, and the person helping me doesn't know Python so well, the actual R code is not in my program. Instead, he opened Notepad, put the R code in there, and saved it as (name).r. When executed, the output is written into a txt file, which Python can then read.
All I have to do is ask Python to ask R to run (name).r
I've tried using subprocess.run. That worked for awhile, and then for some unknown reason stopped working and now does nothing. Then I tried using rpy2, which also worked for awhile; but now it looks like the installation is broken and I'm having trouble getting it reinstalled.
I'd like to give a 3rd option a try now: PypeR. I used pip install pyper. Looked like it was successful.
To keep things simple, I opened Notepad and typed in the following, and saved it as hello.r:
message <- 'goodbye'
write.table(message,'C:/Users/(my name)/Desktop/(folder)/goodbye.txt',row.names=FALSE,col.names=FALSE)
Manually opening R and copy-pasting the lines in one-at-a-time does indeed work. But I'm having trouble getting it to work from Python. Here are some things I've tried (I always put import pyper at the top):
pyper.runR("source('C:/Users/(muy name)/Desktop/(folder)/hello.r')")
This gives NameError: name 'dump_stdout' is not defined
pyper.R("source('C:/Users/(my name)/Desktop/(folder)/hello.r')")
This gives FileNotFoundError: [WinError 2] The system cannot find the file specified
r=pyper.R("C:/Program Files/R/R-3.4.1/bin/i386/Rgui.exe")
r("source('C:/Users/(my name)/Desktop/(folder)/hello.r')")
This causes RGui to open up with a blank R Console. And then nothing happens. When I click back to Python, the console shows Python is busy until I click the halt button, whereupon I get "OSError: [Errno 22] Invalid argument
What is the correct way to execute hello.r?
Thank you
Looks like I got it. This works:
r=pyper.R(RCMD="C:/Program Files/R/R-3.4.1/bin/R")
r.run("source('C:/Users/(my name)/Desktop/(folder)/hello.r')")

Python 3 from CMD

I am attempting to run this .PY file from Command Prompt:
# Merge two .BSG files
# Starting block and world position are taken from the first file
# Example: "bsgmerge input.bsg output.bsg merged.bsg"
import io, sys
one = open(sys.argv[1]).readlines()
two = open(sys.argv[2]).readlines()
for n in [1,3,5,7,9,11,17,19,21,23]:
one[n] = one[n][:-1]+"|"+two[n].partition("|")[2]
open(sys.argv[3],"w").write("".join(one))
It is a program that takes a creation from the game Beseige and merges it with another saved creation so that opening the merged file results in both creations being present. If you want more details, you can read up on that here.
I am having trouble figuring out how to call this program from the command line. At first I thought the problem was me having Python 2 (it requires Python 3), so I uninstalled 2 and installed 3. This did not help.
What I am doing is entering the "python" command to pull up the Python environment within CMD, then entering the command to call the program based on the third comment in the file ("bsgmerge input.bsg output.bsg merged.bsg").
I tried using full file paths or simply changing to the correct directory before typing the "python" command and using only the file names, but so far I've had no luck.
When I am in the correct directory, then enter the Python environment, typing the command "bsgmerge 1.bsg 2.bsg M.bsg" (my existing files to be merged are 1.bsg and 2.bsg), this error occurs:
File "<stdin>", line 1
bsgmerge 1.bsg 2.bsg M.bsg
^
SyntaxError: invalid syntax
I took a Python course (which is why I used to have Python 2 on my machine) last fall, so I noticed that there is no "def" defining a function in the above code, which is something I've never encountered, so I'm thinking that is the root of my problems.
Thanks in advance for the help.
I was probably same problem with python launcher.
If you use Linux, first line shoud be:
#! /path/to/your/python/3
In Windows it some more complicated:
In registry by regedit change
HKEY_CLASSES_ROOT\Python.File\shell\open\command from "C:\Python27\python.exe" "%1" %* to "C:\Windows\py.exe" "%1" %*.
And first line of script shoud be:
#! python3
Now it shoud work properly.

Call a cmd.exe bat.-like command with python

i need some help with this...
I have a program installed on my computer that i want to call to calculate some things and give me an output-file...
in Matlab the command "dos()" does the job giving me also the cmd screen output in matlab.
I need this to work in python but i am making something wrong.
data='file.csv -v'
db=' -d D:\directory\bla\something.db'
anw='"D:\Program Files\bla\path\to\anw.exe"' + db + ' -i' + data
"anw" output is this one:
>>> anw
'"D:\\Program Files\\bla\\path\\to\\anw.exe" -d D:\\directory\\bla\\something.db -i file.csv -v'
## without the "" it does not work either
import subprocess as sb
p= sb.Popen('cmd','/K', anw) ## '/C' does not work either
i get the following error message from cmd.exe inside the python shell
Windows cannot find "\"D:\Program Files\bla\path\to\anw.exe"" Make sure you typed the name correctly, and then try again.
this line runs when i make a bat. file out of it.
it runs in matlab via "dos(anw)" so what is wrong here?
ps: i have blanks in my command... could this be the problem? i do not know where the first "\" comes from in the cmd. exe error message
for now i created a bat. file with all the stuff cmx.de should do in the specific directory where the input file lies...
i just had to tell python to change directory with
import os
os.chdir("D:\working\directory")
os.system(r'D:\working\directory\commands.bat')
it works good and gives me the output of cmd directly in the python shell

Python - L error in declaration

I have tried to use this code: http://www.cgtk.co.uk/data/gemf/generate_efficient_map_file.py to create GEMF file from map tiles. Problem is, when I run this code with specific folder as a parameter, eg. "py generate_efficient_map_file.py Mapnik", I get error on line 6.
File "generate_efficient_map_file.py", line 6 file_size_limit = 2000000000L
And mistake is in word L. How to solve this, when there is a declaration error?
Thx
Try running it like this
python generate_efficient_map_file.py dirname
This works fine for me (no line 6 error - I don't have input data) on python 2.7.3. You can check your python version with
python -V

Categories