I'm writing just a simple Python 3 program (beginner here) and I want to syntax check the code rather than just run it and see errors. How can I do this?
I've done python3 -m py_compile sender.py but nothing changes or is displayed. I'm using version 3.5.2 in Ubuntu, and sender.py is in the directory that I'm currently in.
There are some Python editors than can do this.
For example, Spyder and PyCharm.
They highlight the code segments that have problems.
If you are looking for tools similar to regular compilers, then you can have a look at this stackoverflow question and this with good answers.
Related
Today it happened to me multiple times. I was using Python 3.4.4 shell on win32. I imported datetime, and did some input like 'print(datetime.datetime.now().date())' and got output right. After a while, I had to type "import datetime" again in order to do similar work (like 'datetime.datetime.now(datetime.timezone.utc)')
I think the similar problem happened to me before with other module?
Can anyone explain? I was looking for answers here and google but with no luck.
Is there a way to run python 2 code within a python 3 file?
I have to use a function that is coded in python 2 and is located in a python 2 file. But i need to import it and use the function within a python 3 file. Is is possible to run that function is like a python 2 mode?
It is not possible to run Python 2 code with Python 3, at least not in general. Although converting by hand is fairly straight-forward.
If you have long files you should also consider using 2to3 which will apply the needed fixes to make your code run with Python 3.
If you already have Python 3 installed you simply have to run the following in your terminal.
2to3 your_file_name.py
Note that sometimes 2to3 will be unable to transpile from Python2 to Python3. If it notices it, it will give you warnings and indicate what lines you have to fix manually.
Although, it can also happen that 2to3 doesn't even notice the output code will not work. This is what happened in the example you gave me in the comments:
input('Type text here: ').encode('utf-8').encode('hex')
This will not work in Python3 for reasons that you can explore here.
The reason 2to3 doesn't realize it is because this is actually syntactically perfectly valid code. And actually you could foreshadow input or str.encode in a way that would make this working code.
In conclusion, sometime you have to read the error and fix the code yourself.
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
Sorry if my questions are extremely stupid, but I'm quite at a loss while trying to use Python from Ubuntu Linux. My friend installed the Pycharm for me. I can go without using any commands, to files-->Pycharm projects---> click on one project and see the files, which I can open.
But I'm lost as of how to compile and run the files and see the output at this step? The .py files saved before (written with hello world and some really simple ones) are opening, but how can I see the output?
Also, when I go to the programs by files-->Pycharm projects---> file1.py, file 2.py...etc., do we code and run them from the terminal, using the Linux features? If not, how can I do all of the coding and running from the terminal?
Many thanks!!!
I'm not sure what you mean but "how can I do all of the coding and running from the terminal?" you can use nano to edit to code and run the code on the terminal too
see this link to know more about nano : HERE
I have found very few libraries in Python able to parse excel files, and none of them were in Python 3.x nor passed with success the 2to3 step.
What would be your suggestions ?
Have a look at Python Package Index (Pypi), section Python 3 .
xlrd3 is a port of xlrd, a python 2.x lib to parse Excel files.
My suggestion would be to contact the authors of the libraries and help port them. It's not horribly hard and quite fun! Your only other option is to use Python 2, and that is obviously not as fun. :)
Possibly you could export to CSV as well, but I guess you would have if that was an option.
xlsxwriter is a good tool I've found, and practically the only one for Python 3 that hasn't been discontinued.
I know it works well with both Linux and Windows--LibreOffice in both Linux and Windows, and Excel in Windows.