Simulate python3 shell commands behavior in windows powershell - python

I work on an api that run python3 scripts like python3 <scriptname.py>. This api initially run on a linux system. Unfortunately I make the local development of it on a windows. Is there a way to simulate this behaviour so that when I run in powershell python3 -V it give me the version of python.
I have allready python 3 installed on my computer. I tried to add the path to my python.exe to the system variables with for variable the string python3. But after registering and restarting powershell and type python3 -V I still get the error that «python3» is not recognized as a command applet name.
Does anyone have any ideas?

Once python is in your path, you can define a PowerShell alias for python3.
PS > New-Alias python3 python
PS > python3 -V
python 3.6.5
In order to have this alias as a permanent one, refer to this question: Permanent PowerShell Aliases.

I found tricky solution.
Once python is in your path, go to python installed path and copy python.exe and named duplicated one as python3.exe

Related

python27 and python3 are not not recognized as an internal or external command, operable program or batch file [duplicate]

I am using Python 3.5.2 version on Windows 7 and tried using python3 app.py. I am getting this error message:
'python3' is not recognized as an internal or external command,
operable program or batch file.
Is there any specific cause about why the python3 command is not working?
I also verified that the PATH is added to environment variables.
There is no python3.exe file, that is why it fails.
Try:
py
instead.
py is just a launcher for python.exe. If you have more than one python versions installed on your machine (2.x, 3.x) you can specify what version of python to launch by
py -2 or
py -3
You can also try this:
Go to the path where Python is installed in your system. For me it was something like C:\Users\\Local Settings\Application Data\Programs\Python\Python37
In this folder, you'll find a python executable. Just create a duplicate and rename it to python3. Works every time.
Python3.exe is not defined in windows
Specify the path for required version of python when you need to used it by creating virtual environment for your project
Python 3
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
Python2
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
then activate the environment using
.\environment\Scripts\activate.ps1
Yes, I think for Windows users you need to change all the python3 calls to python to solve your original error. This change will run the Python version set in your current environment. If you need to keep this call as it is (aka python3) because you are working in cross-platform or for any other reason, then a work around is to create a soft link. To create it, go to the folder that contains the Python executable and create the link. For example, this worked in my case in Windows 10 using mklink:
cd C:\Python3
mklink python3.exe python.exe
Use a (soft) symbolic link in Linux:
cd /usr/bin/python3
ln -s python.exe python3.exe
In my case I have a git hook on commit, specified by admin. So it was not very convenient for me to change the script (with python3 calls).
And the simplest workaround was just to copy python.exe to python3.exe.
Now I could launch both python and python3.
If python2 is not installed on your computer, you can try with just python instead of python3
For Python 27
virtualenv -p C:\Python27\python.exe django_concurrent_env
For Pyton36
virtualenv -p C:\Python36\python.exe django_concurrent_env
Enter the command to start up the server in that directory:
py -3.7 -m http.server
I had a related issue after installing windows 11, where python3 in cmd would open the windows store. I was able to sort it out between this post and this other one. In short, I reinstalled python and made sure to add it to PATH. Then, in settings, Apps > Apps & Features > App Execution aliases. Here, all I had to do was make sure that every single python .exe (including idle and pip) were turned off EXCEPT FOR the python3.exe alias. Now it works like a charm.
FWIW:
The root of this issue is not with you or with python. Apparently, Microsoft wanted to make installing python easier for young kiddos getting interested in coding, so they automatically add an executable to PATH. For those of us that already have this executable, it can cause these issues.
Found out instead press the play button the top right and it should work in visual studios:
Do not disable according to first answer
Saying python3 in the command will not work by default.
After figuring out the problem with the modules (Solution): https://youtu.be/paRXeLurjE4
Summary:
To import python modules in case of problem to import modules:
Hover over python in search:
Click open in folder
Hover over and right click
click properties
copy everything in path before \python.exe
close those windows
For cmd (administrator):
cd --path that was copied--
then python -m pip install --upgrade pip
cd Scripts
pip install "Name of Package" such as pip install --module (package) --
Im on win10 and have 3.7, 3.8 and 3.10 installed.
For me "python" launches version 3.10 and does not accept commands (like -3.7), "py" launches newest version but does accept commands, and "python3" does nothing.
Uninstalled 3.10 and "python" now does nothing, and "py" launches 3.8.
I am unable to add a comment, but the mlink option presented in this answer above https://stackoverflow.com/a/55229666/8441472 by #Stanislav preserves cross-platform shebangs at the top of scripts (#!/usr/bin/env python3) and launches the right python.
(Even if you install python from python.org, Windows will direct you to the app marketplace nowadays if you type python3 on the command line. If you type python on the same cli it will launch the python.org version repl. It leads to scripts that generate no output, but more likely silently failed completely. I don't know ho common this is but have experienced it on a couple of different devices)
If you have this at the top of your script to ensure you launch python3 and don't feel like editing everything you own, it is not a bad approach at all... lol.

Shell is selecting Python 2.7, but not 3.7

I have Python 3.7 and I had a Python 2.7.
But when I am using Python Launcher it uses 2.7 not 3.7.
How to use Python 3.7? OS - Mac OS.
If you want python 3.7 as default when you run command python in terminal, you can add an alias in ~/.bashrc
alias python=python3.7
bashrc is a shell script that bash runs whenever it is started interactively. You can find bashrc in your home directory.
In the Python Launcher menu, go to Preferences. You should see a text box called "Interpreter", in there insert the path to your python 3.7, for example:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
You might also add that to your PATH environment variable and use python3 (and idle3) rather than plain python.
There are several ways you can obtain this behavior, my preferred one is symlinking the python3 executable to python
sudo unlink /usr/bin/python
sudo ln -s /usr/bin/python3 /usr/bin/python
Another way would be using pyenv https://github.com/pyenv/pyenv
Be aware od the fact that if you do this and you have some scripts on your machine that use the shebang #! /bin/python and were written for py2, they may stop working

How to change python version in command prompt if I have 2 python version installed

I have installed Anaconda 2 & 3 in my system. Anaconda 2 contains python 2.7 & Anaconda 3 contains python 3.6.
I need to run my python code using command prompt & I need to use python 3.6
While I'm running python --version, I'm getting python 2.7.14. How do I change it to python 3.6?
As you can see, I have both Python2 and Python3 installed.
I hope you know that the path of the python executable has to be added to the PATH environment variable in Windows. As you can see, the path of Python2 is placed above the path of Python3 in my system.
How does the cmd run commands?
It searches for an executable with the same name in the directory the cmd has been opened in, then goes and searches for the command in the locations provided in the Windows PATH variable, from TOP to BOTTOM.
Which means, it gets the path to Python2 before it can get to the path of Python3. Therefore, every time you type python in your cmd, it runs Python2.
Both Python2 and Python3 executables have the same name in Windows so it never runs python3.
What seems like an obvious solution?
You might think, changing the name of python.exe to python3.exe for the Python3 executable will solve your problem. You are partially right, it will work. But you have to use python3 file.py or python3 --version, which I think, is understandable. But, pip will no longer work if you change the name of the original python executable. You will no longer be able to install external packages and modules.
How can this problem be solved?
You can create an alias for the Python3 executable called python3.bat.
.exe and .bat files can be called from the cmd directly without using their extension. We always write python filename.py instead of python.exe filename.py although both are correct. The same can be done with .bat files.
Go back to the first image and notice the python3.bat file below python.exe. That is my way of calling python3 without renaming my original python executable.
python3.bat
Create a new file using notepad or something and paste this %~dp0python %*
I don't fully understand how this works except that dp0 basically runs python from inside the same directory and %* passes all the arguments to the python executable. Place this file inside your Python3 installation directory and your problem will hopefully be solved.
python3 basically runs your python3.bat file, which in turn runs the python.exe from its folder and passes the arguments to it.
I hope this solves your problem.
You should have python3 also in your path. You could use python3 to run your script:
python3 <your_script>
But to answer your question, you could use alias and update it as python.
$ python --version
Python 2.7.6
$ alias python=python3
$ python --version
Python 3.6.4
If you want to run Python 3.6, then execute python3.6.
Otherwise, check with which python where the symbolic link to the actual python executable is. On my system, it's
/usr/bin/python
and when I ls -la /usr/bin/python, it gives
lrwxrwxrwx 42 user user 42 Aug 24 03:45 /usr/bin/python -> python2.7
If you are really sure that you want to do it, you can now just move the old symlink somewhere:
sudo mv /usr/bin/python /usr/bin/old_python
and create a new symlink:
sudo ln -s /usr/bin/python3.6 /usr/bin/python
Now you should get something like:
$ python --version
Python 3.6.5
I don't guarantee that it doesn't break everything in the world, especially if you have some distro that has a package manager that relies on a specific python version under python.
This seemed to work nicely on Linux Mint 18.
I made a small trick to solve this problem. For me
C:\Python27
and
C:\Python27\Scripts
were at the top of the PATH Environment variable list. I simply selected them and clicked on Moved Down to shift them to the end of the list.
Just make sure that your path of Python3 and its Script folder is above Python2 path.
And it solved the problem for me. Hope it would help you too.
Reason :
How does the cmd run commands?
It searches for an executable with the same name in the directory the cmd has been opened in, then goes and searches for the command in the locations provided in the Windows PATH variable, from TOP to BOTTOM. Which means, it gets the path to Python2 before it can get to the path of Python3. Therefore, every time you type python in your cmd, it runs Python2.
Both Python2 and Python3 executables have the same name in Windows so it never runs python3.
reason is taken from the accepted answer to this question.
In order to run your script with python 3 you can use
python3 <path to file>
P.S.: this should be a comment, but I do not have the required reputation. :)
If Anaconda 3's is the only python3 interpreter in the system you can run python3 instead of python in your command line and it should work.
You can alter PATH parameter inverting the positions of both interpreters.
I have two versions of the python which the first path is python 2(I am still working with python 2), but there are some scripts which should use python 3. This is what I have done:
I have create a bat file.
ex:
open Notepad++ and write :
#echo off
set PYTHONPATH=
"%~dpn0.py" %*
save it as 'yourscriptname.cmd'.
before my script(which should use python3) I just run this file in command line and then run my script. it is working.

How to load non-default version of python in command line

I have 2 different versions of python installed on my system i.e. 2.6.6 and 2.7.9.
I am running python scripts on linux via QTP(using paramiko on windows). I execute the following sample of code to execute commands.
"python /dir/of/frameword/ somecode"
When I hit this command the default version of python is used to create the shell that is 2.6.6
Is there any way that I could instruct the linux to pick up 2.7.9 with a command like "python27".
I have also added an alias in ./bashrc but that also dosent help.
set up a shell alias
alias python=/usr/local/bin/python2.7
PS:have you run source ~/.bashrc after save ~/.bashrc?I noticed you mention ./bashrc instead of ~/.bashrc,is it a spelling mistake?
use virtualenv

Python 3 installation on windows running from command line

Just curious, is there a particular reason why Python 3.x is not installed on Windows to run default with the command line "python3", like it does on Mac OSX and Linux? Is there some kind of way to configure Python so that it runs like this? Thanks.
EDIT: Just to add, the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command "python".
the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command "python".
To run Python 2 executable:
C:\> py -2
To run Python 3 executable:
C:\> py -3
where py is a Python launcher that is bundled with your Python 3 installation.
py recognizes the shebang (e.g., #!/usr/bin/env python3 causes Python 3 executable to be run), it respects virtualenv (if you run py without specifying the explicit python executable version) i.e., run:
C:\> py your_script.py
and the correct python version is used automatically -- you don't need to specify the Python version on the command-line explicitly.
is there a particular reason why Python 3.x is not installed on Windows to run default with the command line "python3", like it does on Mac OSX and Linux?
OSX and Linux have python executable installed by default as a rule and it refers to Python 2 version in most cases at the moment that is why you need a separate python3 name there.
There is no Python on Windows by default. And therefore any version that you've installed is just python (I guess). The recommended way to manage multiple python versions is to use the Python launcher.
Is there some kind of way to configure Python so that it runs like this?
If you want to type python3 some_script.py instead of py some_script.py or even just some_script (assuming .py is in %PATHEXT% and Python launcher is configured to run Python scripts (check assoc .py and ftype Python.File) -- the default) then create a bat-file e.g., python3.cmd and put it in %PATH%:
"C:\path to\Python 3.X\python.exe" %*
You likely missed the checkbox at the bottom of the installer.
Full documentation here: https://docs.python.org/3/using/windows.html
Then, I think you just run python, not python3 from the Command Prompt. The reason Unix systems have python3 is because python defaults to Python2.x in many systems.
You have to add the python bin folder to your path. You can do it manually but when you install python i remember you have an option to do that.
I work with multiple Python 2.x and 3.x distros on Windows. Some of them are "portable" - i.e. not recorded in the Windows registry, and therefore not accessible by the version-selector py.exe delivered with Python 3.3+. To save my sanity, I wrote SelectPython.bat which is available on bitbucket. It configures the PYTHONHOME, PYTHONPATH and PATH variables according to the target you give it (a relative or absolute path to the parent directory of python.exe). You can do so in a way that is sticky for the rest of your command-line session:
> SelectPython C:\Path\To\Desired\Version\Of\Python
> python
or transiently, i.e. to call a particular python command without otherwise affecting the environment of the shell you're calling it from:
> SelectPython C:\Path\To\Desired\Version\Of\Python python -c "import sys;print(sys.version)"
You may find it helpful.

Categories