My python 3 code is being run in python 2 - python

I've been writing code in python 3.3.5, but whenever I run one of my py files, it is always ran in python 2. I used to have python 2.7 installed, but then I uninstalled it, so I don't know why this is the case. Even when I type in "python" on command line, it comes up with "python 2.7.3 default", even though I deleted it. I can prove this because one of my scripts is this:
print("Hello)
input()
print("Bye")
input()
It says hello, then gives a traceback error when I hit the enter key. But if I change the coding so that the inputs are instead raw_input, then it works fine. Also Tkinter works instead of tkinter. Yet I am coding this all with python 3.3.5

Since you mentioned having problems assigning your PATH variables to the correct execution location in Windows, you might try the following:
Edit your environment variables
Scroll down to Path, select path, click edit.
Add C:\Python27; You should in your case have 2 versions of python there. If you don't you could add the following:
C:\Python27;C:\Python335;
Navigate to the Python27 folder in C:\ and change python.exe to python2.exe
Navigate to the Python335 folder in C:\ and change python.exe to python3.exe
Open up command prompt and type python2
Then, in command prompt, type python3
You should see different output at this point denoting that you have different versions active.

Related

Unable to change Python 2.7 to Python 3.8 on Sublime and VS code

I have been trying to change my python interpreter on sublime text and VS code and no matter what I do, both of them refuse to change. It always picks the default Python 2.7 version.
On sublime, I tried to create a new build tool, added the path to Python3 but nothing is working.
And on, VS code, even if I select Python3 from the drop-down menu, it doesn't change anything.
I changed the system's default python interpreter from 2.7 to 3.9 and set the path on bash profile. It fixed the issue for me.
I don't know about Sublime, but for VS Code, there looks to be quite a few things to verify:
Check your User Settings on whether you've set up a default interpreter for your applications
Are you using Window 10 WSL? Then you might want to check out issue 3227 for vscode-remote-release where the comment was:
So I was having this same exact issue until I read this article on a wsl tutorial. In there it says we should have "Remote - WSL" extension installed (already did). In the bottom left corner "Remote - WSL" installs a status button that has the "greater than" and "less than" symbols displayed. If you click that button and tell VSCode to open up in the remote WSL distro environment it fixed my issue where vscode was unable to find an interpreter for python.
For Sublime, you need to hit ⌘B with your script pane focused in order to use your new python3.sublime-build build system.
It looks like you are running your script within Terminus, which is fine, but you need to use the python3 command when running it from the command line, even if that command line is within Sublime.
Please check out my answer here on how to make a Python build system that accepts user input, as the Sublime "console" doesn't allow you to interact with your program. Since you already have Terminus installed, I'd recommend going down to the bottom of the answer and using the Terminus build system. That way, you won't have to deal with Terminal windows opening every time you run a build.
The terminal window uses your PATH, not the IDE settings, so manually typing out python --version isn't testing anything except the PATH variable
In order for the terminal to use the "correct" python binary you've specified, it would need to give the absolute path to it, which I believe using Command+B (using the build options) in Sublime would do. Similarly, you can use the Run/Debug Configurations in VSCode.
Or you can modify your ~/.zshrc to fix your PATH, for example installing pyenv to change the entire system Python version, or the one for the current folder

I can't get the python commandline program to work. In spite of having set the PATH variable

Quick question regarding the installation of python. I've installed Python 3.7.7 using the Windows 64 bit installer. I also had the installer update the PATH variable for me.
But when doing a quick check with the command-line using the command: "python --version", I'm getting nothing.
Nothing. No error, just an endline.
User Path variable System Path variable command prompt
Edit(1):
By going to the directory with python.exe within the terminal using the command "cd C:\Users\Andrei\AppData\Local\Programs\Python\Python37\" and then using python --version. The command works. I'm receiving the python version 3.7.7 message.
In spite of the path variable pointing to the same folder though, the command still doesn't work when I'm outside it.
A very weird thing I realized is that the command "py" does have some output. Regardless of what directory I'm in.pyOutput
So that command works, while python --version doesn't. In spite of python --version working when I'm within the actual directory of python.exe.
I would assume that while I'm outside the python directory, there's some other executable that the system is accessing first? Regardless, I need all the functionality of python to work as I'm trying to get an external program to work with it and within that program, I'm getting the error that python is not installed. "Download python and add it to the path variable" it still says...
Edit(2)(SOLVED!):
There must've been some other folder within the path that had a python command that was overriding this one, as now it works and all I changed was the position of the folder within the path variable. It previously sat lowest, now it is the first one. And the command always works as intended, regardless of what directory I'm in.previous position new position
First, be sure you added the path to environment variable correctly
for testing, you can open your terminal in your path that you added to the environment variable and run python --version again.
If that not work for you try these solutions:
Close your terminal and open it again
or
try python3 --version instead of python --version
It's probably because you don't have Python installed through the Microsoft Store. You need to write "python" in the cmd and then a Microsoft Store window will pop up and let you download it.

Django manage.py [duplicate]

Today when I tried to run simple code on Sublime Text 3, the following message appeared:
Python was not found but can be installed from the Microsoft Store: https://go.microsoft.com/fwlink?linkID=2082640
And when I type Python in CMD, it opens the Windows Store for me to download Python 3.7. This problem started today for no good reason. I didn't change or download anything about Python and already tried reinstalling Python, and the Path environment variable is correct.
Use the Windows search bar to find "Manage app execution aliases". There should be two aliases for Python. Unselect them, and this will allow the usual Python aliases "python" and "python3". See the image below.
I think we have this problem when installing Python because in a new Windows installation the aliases are in the ON position as in image below. When turned on, Windows puts an empty or fake file named python.exe and python3.exe in the directory named %USERPROFILE%\AppData\Local\Microsoft\WindowsApps. This is the alias.
Then Microsoft put that directory at the top of the list in the "Path" environment variables.
When you enter "python" in cmd, it searches the directories listed in your "Path" environment variables page from top to bottom. So if you installed Python after a new Windows 10 install then get redirected to the Windows Store, it's because there are two python.exe's: The alias in the App Execution Alias page, and the real one wherever you installed Python. But cmd finds the App execution, alias python.exe, first because that directory is at the top of the Path.
I think the easiest solution is to just check the python.exe and python3.exe to OFF as I suggested before, which deletes the fake EXE file files. Based on this Microsoft Devblog, they stated they created this system partially for new Python users, specifically kids learning Python in school that had trouble installing it.
Creating this alias was to help kids just starting Python to install it and focus on learning to code. I think Windows probably deletes those aliases if you install Python from the Windows App Store. We are noticing that they do not get deleted if you manually install from another source.
(Also, the empty/fake python.exe is not really empty. It says 0 KB in the screenshot, but entering "start ms-windows-store:" in cmd opens the Windows App Store, so it probably just has a line with that and a way to direct it to the Python page.)
Finally, as Chipjust suggested, you can create a new alias for Python using something like DOSKEY as explained in this article for example:
How to set aliases for the command prompt in Windows
The main problem here is that the order in the path calls the windows from top to bottom, and that there is python.exe in %USERPROFILE%\AppData\Local\Microsoft\WindowsApps which is called first if there are no other python.exes in the PATH above that line.
To ensure that the correct python.exe is called, add the Python interpreter installation folder (containing python.exe) to the PATH, above %USERPROFILE%\AppData\Local\Microsoft\WindowsApps
Here is an example:
To get to this location, click "Start" → start typing "Env" → Select "Edit the system environment variables" → "Environment variables" button → Select the entry for "Path" in the upper list → Click "Edit".
Python components should be at the top, as in step 5. If not, move them up by pressing the button in step 6.
If the Python interpreter is already installed, then go to Apps & features from settings, select Python, and then select modify.
Again select modify and select Next:
Then this window will appear:
Select "add Python to environment variable" and click on the install button. Then again go to apps & features, click modify and click Repair.
Now go to CMD and type Python.
Problem solved.
This is a PowerShell script that does the magic.
Remove-Item $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps\python*.exe
I had problems with this as well, where Windows didn't recognize Python or Anaconda in a double click or cmd (command) prompt.
Problem: unable to import libraries in "python" cmd in Windows. Instead the Windows "python" cmd took users somewhere they don't want to go.
Problem Cause: In Windows "Environmental Variables", Windows adds a python.exe and python3.exe (I don't know where these link to) in the "%USERPROFILE%\AppData\Local\Microsoft\WindowsApps" directory.
Solution: I tried deleting the python*.exe files in the WindowsApp directory, but Windows wouldn't allow it, so I opened a command prompt in the "%USERPROFILE%\AppData\Local\Microsoft\WindowsApps" directory and typed:
del python.exe
del python3.exe
Then I created an environment variable linking to the installed Python interpreter link. In my case, it was C:\Anaconda3; C:\Anaconda3\Scripts, and some others for good measure.
Because this is a common issue and this appears to be the canonical question, I want to try to give a complete overview of The Python 3.7 Windows Store Fiasco (TM).
Why is it possible for this to happen?
A convergence of two things: the previous introduction of the Python Launcher for Windows in 2011 (hereafter py), and a Windows 10 update in May 2019 that was apparently intended to make installing Python easier for Windows users.
Oops. Turns out that installation path isn't great; it bypasses the "lengthy" setup wizard... which contains some options that some users find very useful. It caused other issues, too. Not to mention that it just works in slightly non-standard ways, has limitations on file system access because it's a Store app, initially couldn't itself be launched by py....
Okay, but why do those factors result in the problem?
Since the introduction of py, by default, Windows Python installers do not add the new Python install to the PATH. Why? Because the entire point of py is that it uses its own logic to find a Python installation, based on some combination of command-line switches and possibly the source file's own shebang line. Now your source files can be associated with py instead of any particular python.exe, and you can get Linux-like behaviour when double-clicking a file. Meanwhile, by running py at the command line, you have easy access to whatever you need, and you don't have to think about which version of Python was installed most recently. So there's seemingly no good reason to put any of those Python installations on the PATH. It only risks confusing you when, for example, the most recently installed version isn't the most up-to-date one. Right?
In the update, Windows 10 put a "python.exe" into a Windows Apps-related folder, which is a wrapper app to open a Microsoft Store link. The idea is that it's on the PATH, but way near the end; so if you have an installed Python, it gets used, and otherwise the wrapper is invoked and helpfully prompts you to install Python - so that you can actually run that random, totally trusted .py file your friend sent you on Discord.
And it would work perfectly, if your installed Python were on the PATH.
Oops.
(But, you know, py had been introduced around 8 years prior. You'd think someone at Microsoft would have been aware of the potential issue. Maybe instead of a special shortcut link, they could have made an actual script that checks for the presence of C:\Windows\py.exe or something.)
So what are my options?
You can check the option to add new Python versions to the PATH when you install them, and deal with the fact that python at the command line means a specific one of them. If you need to change that, you can manually tweak your PATH variable.
You can just manually tweak the PATH variable after the fact. (or "Modify" a Python installation to fix it.) This is covered in several other answers.
Independently of that, you can disable the wrappers, as shown in the top answer. You should probably do this anyway; seeing python fail at the command line is less aggravating than dealing with a random GUI window popping up and offering to install something for you, especially when you know you have it already.
If you want to keep the PATH empty, consider using virtual environments for your projects. Whenever a virtual environment is active, the PATH is temporarily modified such that python means the Python installation of that environment. It's quite convenient, really.
You might be able to tell your IDEs to use py instead of a specific Python installation, and it might even be helpful to do so. I don't know. I don't use one.
As a person who does Python development in Sublime Text, I know you said the Python interpreter path was correct, but when you install the Python interpreter make sure to tick the option to add Python to PATH.
I had the same issue back in the day till I did this.
You can manually add Python to the Windows path by doing this:
Start the Run box and enter sysdm.cpl
Go to the Advanced tab and click the Environment Variables button
Now you’ll need to locate the relevant Python paths
Here is how a Python application path looks like:
And this is how a Python Scripts path looks like:
Select the Path variable, press edit and add both paths (Python application and Python Scripts)
If it doesn't show, press new instead of edit and fill the New User Variable box
This is how my Variable value looks like:
C:\Users\Ron\AppData\Local\Programs\Python\Python37-32;C:\Users\Ron\AppData\Local\Programs\Python\Python37-32\Scripts
That’s it! You just added Python to the Windows Path.
Source: Data To Fish
So, I had the same problem.
My answer was to add python to PATH not only for User variables, it was there, but also for System variables. And now everything works.
So, I've got the same problem in VENV.
I solved it by typing >> python**.exe**
But to add every time .exe a little bit nervous.
Also try to type python.exe in cmd.
This is an additional note for anyone using pyenv-win: after turning off the application execution aliases, run pyenv rehash. You may also need to close/reopen your CMD or PowerShell window.
I have put together a powershell snippet, which reorders WindowsApps and Python folder so that Python is first. Run this as Admin:
$appsFld="$env:USERPROFILE\AppData\Local\Microsoft\WindowsApps";
$pyPath=(Resolve-Path "$env:USERPROFILE\AppData\Local\Programs\Python\Python*\")
$Env:Path = (($Env:Path.Split(';') | Where-Object { $_ -ne "$appsFld" }) -join ';');
$Env:Path = (($Env:Path.Split(';') | Where-Object { $_ -ne "$pyPath" }) -join ';');
$Env:Path += ";$pyPath";
$Env:Path +=";$appsFld";
[Environment]::SetEnvironmentVariable("PATH", "$Env:Path", "Machine")
Then I can run python just fine:
> python
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> quit
Adding a bit to the question. Even when I typed pip freeze, it wasn't showing anything.
Here's what I did:
There were multiple instances for the Python application in /AppData/Local/Microsoft/WindowApps.
I deleted those and then it worked.

Python 3 install error. This file does not have a program associated

I have encountered an error while try to run a python 3.6.4 script after upgrading from 2.7.
I had initial installed both python 2.7 and 3.6 but them deleted 2.7 because I had no use for it and i am not getting this error.
I have tried:
Reinstalling python 3.6.4
changing the install location of said install
Repairing the python 3.6 install
Error:
A Python script by itself is not an executable program. So if you want to run the script Evosim.py by just entering Evosim.py in the command line, the .py file ending needs to be associated with the Python interpreter. It seems that currently in your system this is not the case.
There are two ways to deal with your situation:
Start your script by entering python Evosim.py instead of only Evosim.py in the command line.
Fix the broken association of the .py file ending with the Python interpreter. The answers to this question migth help you with that.
Solution #1
Go to your registry by searching regedit.
Open category HKEY_CLASSES_ROOT. Select .py. If .py don't exists then make it by yourself.
Double click the Default. It will make a popup. Change its value to Python.IDLE and click OK. Make a new String key in .py naming Content Type with value text/plain.
Solution #2
If the above method don't works then just go ahead and reinstall your python (also uninstall Python Launcher). Your modules and files will be at their place.
You can now open .py files.
Running command prompt as an administrator resolved my issue, and able to execute my python script
1). Right click any python file
2). Click Open with then Choose another app
3). Click on More apps then Look for another app on this PC. It should open up file explorer
4). Select python.exe wherever it is located.
A sample path is C:\Users\<user>\AppData\Local\Programs\Python\Python38\python.exe for a user installation. A way to check this path is by typing where python into cmd; it will list all installations of Python.
Now the python.exe you chose is associated with all .py files and typing file.py should not give an error.

How to get pydoc command working in Windows 7 cmd?

I'm learning to program Python from the "Learning Python the Hard Way". On one of the chapters it tells me to use pydoc command. The windows 7 cmd failed miserably with that command.
When I typed in pydoc raw_input() on cmd following error codes were generated:
'pydoc' is not recognized as internal or external command,
operable program or batch file.
I've looked back previous two questions on same issue:
Pydoc is not working (Windows XP)
How to get pydoc command working in Windows?
So far I have created a pydoc.bat with following line
#python c:\Python26\lib\pydoc.py %*
and saved it in C:\python27\Tools\Scripts.
I've also tried to change the PATH to C:\python27\Lib. I called the two variables python and pydoc because I'm not really sure I set the PATH correctly.
It's still not working.
What did I do wrong?
To get help on python when in the PowerShell window, type:
python -m pydoc raw_input
substituting your function name for raw_input.
Hey I know this post is a bit old, but I wanted to let you (and anyone else out there) know about this:
C:\Python27\Tools\scripts\pydocgui.py
It sets up a webserver on localhost:7464. Simply visit the address in your browser and you have access to pydoc :) Hope this helps someone
You added this to your script file:
#python c:\Python26\lib\pydoc.py %*
But it seems that you are using python 2.7
I run a Windows 7.
I am using the command prompt, cmd.
I am learning Python from Learning Python the Hard Way.
I was having the same problem as you, and attempted to solve it on my own from what you gave me.
Here is what I got:
On gedit, I typed
#python C:\Python27\Lib\pydoc.py %*
It is important to note two parts:
The C:\ is capitalized, the L in Lib is capitalized.
Both of those were left under-cased, and that created a syntax error when I tried to run it.
I saved this as
pydoc.bat
from gedit in C:\Python27\Tools\Scripts.
From cmd, I cd'd into the above address. From C:\Python27\Tools\Scripts I typed pydoc.bat open
and it worked. It showed me the information I needed.
The only difference between what you did and what I did seems to be the capitalization of certain letters.
Check your typing and ensure it fits what the computer needs to see.
Copy paste the bat file in this path.
C:\python27
and then run it as :
pydoc.bat raw_input
this should work. make sure that the PATH is set to:
C:\python27
This will help you to run the bat file from anywhere in the cmd line
set the path as
C:\Python27>python (or wherever you installed python)
Then write command -m pydoc raw_input
which will look as
C:\Python27>python -m pydoc raw_input
Note: There's a space between python and -m
There are several ways to fix it:
Check if path you've added to PATH is in there.
Type in command line PATH. If path you've added isn't there then Restart Windows.
If it is go next line.
Type pydoc.py instead of pydoc
If you don't want type pydoc module extension every time, just add .PY extension
to PATHEXT variable located at the same place as PATH variable does
(Computer → Properties → Advanced system settings → Advanced → Environment Variables...
→ System Variables → PATHEXT). Restart your Windows after adding of extension.
Type Pydoc.py x replacing x with whatever document you want to read.
For example Pydoc.py raw_input (in cmd ofcourse)
Do not forget to change path to the lib directory. do it like this:
Right click on my computer>properties>Advanced tab>environment variables> search for the value PATH in system variables>edit> enter this -> ;C:/Python27/lib
Here's how you do it from zero/layman (my kind of) condition, learning python:
If you have a Win7 32bit system with no environment variable set manually after installing (basically nothing else done after just installing it) python 2.7 or any version lower than python 3 from website, then follow these steps:
1) Open Command Line, and type (in exact case):
cd\
until you come back to "C:\" directory (root/base directory where your python is installed).
2) Then type:
cd Python27
As my base dir or drive is C drive, my path is this, yours may be different
hence you type the route of Python27 (or 26 or the name of your python folder) folder where you have installed Python2.7 (or whatever version).
3) Then type:
cd Lib
You just entered the Library folder where pydoc.py file is present as per default installation from internet (for me it was the case).
4) Then simply type:
pydoc.py
You will get the required pydoc output which seems to be of the kinds of "man" page of PERL/Linux or the "Help" of C/Windows.
5) Then simply type:
pydoc.py raw_input
Name of the required command for which you want to see documentation.
Now you know how to do it for all the rest of the commands.
you must not be in correct directory in your command prompt while typing "pydoc raw_input"
first type:
cd\python27\lib
then type:
pydoc raw_input
I have the answer: see the thing is we been running pydoc raw_input in the cmd, but what we where forgetting is that to run python files we have to type .py at the end of the file
im using windows 7 NVidia, to run you got to be in it's Dir C:\Python27\Lib, then type pydoc.py and enter.
There you go hope this was helpful.

Categories

Resources