Python and VS Code, messy paths in terminal - python

I've just started with Python, I've installed Python and it's extension from Microsoft, and when I run Python file in terminal I get the results, but the ugly path is covering plenty of space in terminal (like on the photo below). I've found video how to remove that from debbuging console, but couldn't find anything related to normal terminal. Is there any solution for that?
I am using Windows 10 and newest version of VS Code.
https://i.stack.imgur.com/vjKOm.png

You need to do 3 things.
1.
You need to add Python to your PATH. Call this python2 if you want to use python 3 as well for other project.
(However if you are starting, considering using python 3 from the beginning)
2.
Just use the file name if you are already in the folder
Your command is reduced to this then:
python2 strings.py
3.
Use prompt to change the line before your command. See doc for more information: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/prompt

Related

Path Error using Python 3 in Sublime Text

I have a struggle using this editor. I tried it a year ago and after some hair-pulling, ended up leaving it for another platform. I came back today, and I want to fix my problem and go farther than just step one.
I got a picture of my code and will provide the code written out here as well. I will also be providing a picture of the book I am using (Python Crash Course 2nd Edition) with the instructions given to me.
If any more info is needed, or some clarification. Please let me know. I want to get moving past this obstacle.
Picture of my code
print("Hello Python World!")
[WinError 2] The system cannot find the file specified
[cmd: ['python3', '-u', 'C:\\Users\\2\\Desktop\\New folder\\Coding\\python\\python_work\\hello_world.py']]
[dir: C:\Users\2\Desktop\New folder\Coding\python\python_work]
[path: C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\2\Desktop\Python\Python310;C:\Users\2\Desktop\Python\Python310\Scripts;C:\Users\2\AppData\Local\Programs\Python\Python310\Scripts\;C:\Users\2\AppData\Local\Programs\Python\Python310\;C:\Users\2\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\JetBrains\PyCharm Community Edition 2021.3.1\bin;]
[Finished]
Picture of Directions from Book (pg 10)
{
"cmd": ["python3", "-u", "$file"],
}
print("Hello Python World!")
Hello Python World!
[Finished in 0.1s]
Once again, if any clarification or extra detail is needed, just let me know and I will clarify to the best of my ability.
Problem solved
Ended up switching the build system from Python3 to Python and removed the cmd directions from the book. Problem fixed. Big thanks!
The instructions in your book are either out of date or flat out misleading you; I would not recommend following them.
You only need to create your own sublime-build file to execute a Python program as Python version 3 if you're running Sublime Text 3 AND you are on Linux/MacOS.
The Python.sublime-build file that ships with ST3 will execute python.exe by default on Windows which (if you have Python installed and on the PATH) will work. The version of the file that ships with ST4 executes the py.exe helper on Windows, which is always available on the PATH and will find the right version of Python for you no matter where it's installed (so long as it is in fact installed).
Using the name python3 is required only on Linux and MacOS, for which the name python refers to Python 2 and not Python 3 (and it does not exist on Windows at all, which is the ultimate source of your problem). In ST3 the build always tries to execute python on these platforms, but in ST4 the default is now python3.
The build as outlined in the book doesn't set up an appropriate selector to allow the build to be auto selected or a file_regex to match errors to allow for error navigation. Perhaps later parts of the book include that; the built in build supports it directly out of the box.
In any case, the most expedient way to test out your code would be to select Python under Tools > Build Systems and then run the build again. Sublime is going to ask you if you want the Python or the Python - Syntax Check build; choose the first one in the list.
The Syntax Check variant only compiles your code but doesn't actually run it, which is almost never want you want.
If that works, you can delete your sublime-build file and change the selected build to Automatic, which will cause Sublime to pick the Python build for you automatically so long as you execute the build while you're editing a Python file.

Having trouble trying to execute a simple HelloWorld.py file from Windows cmd

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.

Python fails silently

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).

Other than swapping order of paths to python folders in system variable 'PATH' - what are the ways to swap python versions?

I can change the order to python2,3 folders in the system PATH variable. But what are other ways to do this?
There should be more elegant way to change versions of python i want to run.
e.g. in console:
python file.py #will run python2
and after i change python command to use python3, it should be the same:
python file.py #will use python3
I suppose you are trying to run your script with the correct interpreter depending on which python version was used. On Unix/Linux this is done with a so called “shebang” which is written in the very first line of the file. E.g.:
#!/usr/bin/env python3.6
If your installation of Python3 is newer than Python 3.3 you can use the python launcher for windows, which should be able to launch the correct version of Python depending on the shebang, even on window.
Also see here for more informations on shebangs.
If your concern is what Python version is executed when calling python in a console, then an alias or a stub script are the two ways to go.
This post will explain you how you can do this on Windows.
The alias way, just like it would be on a Unix system, is to create an alias, either temporary to the session or permanent, so that python now means C:\Python27\python, or whatever version you want.
The script approach consists in putting a script named python in a directory referred to in your PATH, and have that script run the right version of Python.
I highly doubt that this will affect all the batch scripts that call python, but it will definitely fire the right Python when you'll type python in a console.
Now, if you're concerned about what version a script is executed with, you can specify an explicit version with a shebang line, or manually select it by right-clicking the .py file and clicking open with.

Using Python 3.1 with TextMate

TextMate seems to use the built-in Python version I assume (sys.path doesn't work). How do you configure it to use 3.1 instead? I've already installed the 3.1 package and I can use IDLE for interactive sessions, but I need to use TextMate now.
Thanks
TextMate uses the value of the TM_PYTHON variable to find the path to the Python interpreter. A good solution is to take advantage of TextMate's ability to define variables like TM_PYTHON on a per-project basis:
Open a new or existing TextMate Project (File -> New Project or File -> Open)
De-select any file in the project list sidebar.
Click on the Get Info (i) icon in the sidebar. A Project Information pane appears.
Click + to add a new variable.
Enter TM_PYTHON in the Variable field and the full path to the desired python in the Value field (for example, /usr/local/bin/python3.1).
Close the Information window and save the Project (File -> Save Project As).
Then you can add files as needed to the project and they will be run under the chosen python with TextMate Python bundle's Run Script command. You might want to save a Python 3 project, say, for running ad-hoc scripts under Python 3. For bigger projects, you'll want to create a separate TextMate project for it anyway.
To change the Python version used globally within TextMate:
From the TextMate menu bar, open TextMate -> Preferences
click on the Advanced pane
click on the Shell Variable tab
click the + to add a new variable
enter TM_PYTHON in the Variable field and the full path to the python in the Value field (perhaps /usr/local/bin/python3.1)
As Alex points out, you may break other TextMate functionality by changing the Python version globally so the per-project change is probably a better solution.
UPDATE (2010-10-31):
There is another approach that may be easier to use for some projects. The Run command in TextMate's Python bundle appears to respect a shebang line in the file being run. So, instead of modifying TM_PYTHON, you can specify the path to the interpreter to be used by including a first line like this:
#!/usr/local/bin/python3.1
# sample code to show version
import sys
print(sys.version_info)
In many case you would prefer not to hardwire the absolute path but manage use through the normal shell PATH environment variable. Traditionally /usr/bin/env is used for that purpose. However when running under TextMate, your shell profile files are not normally used so any changes to PATH do not show up there including possibly /usr/local/bin or /opt/local/bin or wherever your desired python3 command is located. To get around that, you can add or modify a global PATH shell variable to TextMate -> Preferences (see above) with a value of, say, /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin. Then you can use a more general shebang line like this:
#!/usr/bin/env python3
(This all seems to work with the most recent vanilla TextMate and its Python bundle: no guarantees about earlier versions or with other Python bundles.)
Late to the party, sorry! I take it you want to run the script using TextMate's 'built-in' interpreter? I've found the simplest solution is to add a shebang, which works extremely well;
#!/usr/bin/env python3
for python 3.1 or;
#!/usr/bin/env python
for default system python, although the latter is superfluous for the exercise.
According to this long thread (which was about Python 3.0, and the TextMate version existing back last spring, but I believe is still valid for Python 3.1 and today's TextMate), you can get it done (e.g. via #Ned's answer), but once you do many TextMate commands may well break (because they're written to use Python 2, and Python 3 is not backwards compatible with Python 2 -- for example, the use of reload, which disappeared in Python 3, is repeatedly mentioned in the thread). Still, it might work if you do not use or need much of TextMate's functionality (LaTeX typesetting for example was mentioned as something that totally breaks once you make TextMate use Python 3 instead of Python 2).
the shebang is the best solution, to see where python 3 is installed type in terminal:
which python3
you will get something like this:
/usr/local/bin/python3
if nothing shows up first install python3
and at the top of your script insert:
#!/usr/local/bin/python3
I wanted to achieve this in TextMate version 2.0.23 with the aim to use python3 as the default.
So this is how I could set the TM_PYTHON variable (based on #Ned Deily's answer above):
Open Textmate
Hit CMD + ; to open the settings
Now Add a new Variable with the + sign
4. In the Terminal.app I typed which python3 which gave me /usr/local/bin/python3.
Now whenever I open a python script and hit CMD + R it will execute as a pyhton3 script by default while "honouring" the installed PIP packages.

Categories