I've seen other answers for this on Stack, but they aren't helping.
I have a Python 2.7 script that I need converted to work with Python 3. It seems like using 2to3 shouldn't be this difficult, but I can't figure it out. In the Windows command line, I try typing "python C:\Python27\Tools\Scripts\2to3.py (path to my file)" and it says "Invalid syntax" and has a little carrot pointing to the C in the first file path.
I also try opening the IDLE shell and typing "$ 2to3 (path to my script)", which also says "Invalid syntax" and highlights the $. So I get rid of the $ and try again, and then it highlights the "2to3" with Invalid Syntax.
I've seen other answers as well, and none come even close to working. I feel like an idiot for not getting this. What am I missing? Can someone explain it like I'm five?
(I'm fairly new to programming, but I've been doing pretty well writing scripts in Python. I have no idea why THIS is the thing I'm struggling with.)
Does this work for you?
python C:\Python27\Tools\scripts\2to3.py -w <your_file>.py
This will make the necessary changes to make you <your_file>.py to make it compatible with python3 and overwrite the original file
See 2to3 documentation
Related
As we can see the difference between the images. In vim datatype and function are of different color. How can i bring color to my python repl.
I want to do it by myself. Please suggest me, how should I do this. Whether I should have to do something with stdout stream object or I have to change something in python interpreter and then build it from source (ohh I have to compile the python from source only to bring this change)
Sorry if there is some error in my question. Hopes you all will understand me.
You should probably have searched a little bit more first, because there are many answers here about this. Perhaps the best ones are:
bpython [NOT on Windows without hack.]
ipython
Change Python interactive prompt ">>>"
Some characters stick to my colorized prompt in Python cmd
Adding colors in python interpreter prompt, does not wrap properly
How to fix column calculation in Python readline if using color prompt
I am very new to python and I am trying to execute a python file called HelloWorld.Py. The code within the file editor is
#! Python3
print('Hello world!')
I am trying to do this from my windows command line. The correct path from what I can tell in my documents application is C:\Users\china\PythonFiles\HelloWorld.py.
As I am following a Udemy course, it is recommended that you type py.exe C:/Users/china/PythonFiles/HelloWorld.py (filled in with my example).
At first, I returned a syntax error for the use of py.exe. Reading online made me confused about what to put in front of C:, and after removing py.exe, and just typing C:/Users/china/PythonFiles/HelloWorld.py returns the error 'Unable to create process using 'Python3 "C:\Users\china\PythonFiles\HelloWorld.py" '
What am I doing wrong? I have researched online for a few hours and I am still no closer to figuring it out. Please help!
Remove the comment, or change it to #!python3. py.exe doesn't like the capitalization.
The comment isn't needed unless you have both Python 2 and Python 3 installed, and it hints to py.exe which Python to use, but requires lowercase python followed by the major and optionally minor version, e.g #!python3.6 or #!python3. If the comment is missing, the latest version of Python installed wiil be used.
I am trying to get started with Python, so I installed Python 3.8, from python.org (on Windows 10). I remembered to check the "Add to PATH" during the installation and I have confirmed that it has been added to path, as seen in the first image.
However, when I try to use any commands, whether it be python --version or python HelloWorld.py, nothing happens. I have tried both the traditional command line as well as Powershell. I have also tried replacing python with python3, the result is the same. As you can see in the second image, I get no errors, just a blank line. So it's not that it cannot find Python at all, it rather seems that something is wrong with the installation.
Have anyone else encountered this kind of behavior? I have tried re-installing Python as well as removing old installations.
Image 1: Python seems to be correctly added to PATH.
Image 2: Python fails to return any output. But also no error. Same thing happens with python HelloWorld.py.
Update 1 - Here is what I have tried so far:
Changing PATH to refer directly to exe file.
Using both Command Prompt and PowerShell.
Rebooting the PC.
Re-installing Python (including removing old versions).
Both the python and python3 command.
Update 2 - NameError: name 'python' is not defined
Update on the update: This was a wild goose chase. You are not supposed to be able to use that command in the interpreter, as described in this post.
So I tried to use the console from the python.exe file instead, and I got the following answer. However, from what I can find on it, it's normally a problem you encounter on elements of the code (like print()) and not on the python command...?
(image removed)
Update 3 - It works (kinda)
If I use the command py --version or py test.py it works. I have no clue why though...
After extensive research, I still cannot find an answer to my own question. But I have found a work around, which minimizes the consequences:
Instead of using python, simply use py.
This will invoke the Python launcher instead of Python itself (from what I've read). For most people this will be good enough, but it is not the same. So it might cause issues and version mismatch in certain scenarios.
Also, despite of this weird behavior, Python seems to run fine in Visual Studio Code, when using the "Run" button (or the py command in the terminal).
I am pretty new to Python and I have been pretty annoyed with this problem. I am not sure if this matters, but I run my .py file with Python 2.7.6 with python installed on my computer, not using it on any online thing or other program. Every time I come across an error, my program works fine until it comes to the error, but the window disappears right before I can possibly read whatever the error said it was... Anyways, I haven't been able to find out what is wrong with my programming, and I am tired of guessing and guessing what is wrong. How can I extend the time so I can read the error message? Or something like that? Thanks
You should run it from command window/terminal instead of double clicking on the file.
Yes, as #shortfellow said, running it from the terminal should work:
python your_file.py
Another tip that helps in Python, if you are having a hard time with errors, is to use iPython for debugging/testing code on the spot. You can give it a try.
http://ipython.org/
I have a question.
I try to learn Python and of all programmer's tools I found Dreampie the most suitable. But, my dreampie always automatically sets Python 2.7. I found this topic and following the instructions, I changed both subp_main.py and subp_main.pyc (based on the first). Yet, it's still Python 2.7.
Telling truth, I suppose there should be some way to choose between the two versions, but I did not found anything in the Net or in program options. According to this topic writing "dreampie python3" inside the program should work, but dreampie inform me that the syntax is invalid (?).
[I quess the solution is very easy, but none of searchable things would help...]
You need to open a Terminal window (or Run dialog Alt+F2) and type in the command as follows and press Enter:
dreampie python3
It's not python syntax but shell syntax so you are running the dreampie application with python3 as argument.