Unable to run .py files from Powershell or VS code - python

I'm learning python and use VS Code as the editor and when I try to run the .py file I get the following message ,
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.

3 Ways to solve this :-
If Python is not installed,then install it from python.org
If its already installed then it might not have been added to path.
To add python to path, search for environment variables in search bar, then edit the path option and add the python installation directory location there.
OR you may just re-install python from python installer and tick the "add python to path" option
Plus I would not recommend using windows store version of python. Just use normal python installer from python.org

Related

Adding path of downloaded Python to Visual Studio Code

Just gone through the process of setting up python and all the installs using cmd and now i want to use
brownie bake
To clone some code and it gives errors as it cant locate brownie... I've set environmental variables for the path (that's how I got it to work in cmd)... I've installed the python extension and chose the python.exe file in my new install in the compiler by clicking the bottom left... How can I make visual studios Code use only the python and all library's I installed?
if i run
python
in the terminal it opens up windows store for python install but i can run my version by using
py
First, Disable the python command to open the windows store:
Search Manage app execution aliases and unselect App Installer of Python:
Second, You need to check which python you have installed your packages to:
And make sure in the VSCode you have selected the right interpreter(In the following picture, they are different).

'Python not found' despite having been installed

I've installed Python's latest version. However, when I write in command prompts
python --version
I get:
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
I tried downloading it directly from the Microsoft Store, however, the problem with that was that I could not very easily create a path for it and ran out of skills.
Anyways I'm trying to get Robot Framework to run just for fun, I'm a complete beginner as you can probably see. What should I do to fix this?
You have to add the path of Python to the environment of your system.
For more details refer: How to add Python to Windows PATH
How to add Python to Windows PATH
There are few ways in which you can add Python to Windows PATH. In this guide, I’ll share with you two methods that you can use to add Python to Windows path:
Via the installation of a recent version of Python
Manual entry of the paths
But why would you want to add Python to Windows path in the first place?
Well, if you try to install a Python package using PIP for example, you may get the following error in the Windows Command Prompt:
‘pip’ is not recognized as an internal or external command, operable program or batch file
To overcome this error, you may apply any of the two methods described below.
Method 1: Install a Recent Version of Python
You can easily add Python to Windows path by downloading a recent version of Python, and then checking the box to Add Python to PATH during the installation.
Before you proceed, you may choose to uninstall your previous version of Python if needed.
In my case, the latest version of Python that was available to download was version 3.7.2.
In the Python installation box, just check the box to add Python to PATH as below:
How to add Python to Windows PATH
Finish the installation, and you should be good to go.
Alternatively, you may manually add the paths into the Environment variables.
Method 2: Manually add Python to Windows Path
If you wish to stick with your previous version of Python, you may apply the steps below to manually add Python to Windows path.
Note that I’ll be using Windows 10 to demonstrate the steps, but similar principles would apply for previous versions of Windows.
Step 1: Navigate to the Windows Environment Variables screen
To navigate to the Windows Environment Variables screen, where you can add/edit your paths, simply right click on the ‘This PC‘ icon. Then, select ‘Properties.’
This PC - Windows 10
Next, click on the ‘Advanced system settings‘
Advanced system settings
Finally, click on the ‘Environment Variables…‘
Environment variables
That should take you to the Environment Variables screen, where you can add/edit your paths.
Click on ‘New…‘ to add the ‘Path’ variable (note that if your ‘Path’ variable already exists, then click on ‘Edit…’ instead):
add Python to path
You should then see the following box, which will allow you to add/edit variables:
New User Variable
Before you type any values, you’ll need to locate the relevant Python paths. The paths that you’ll need to get are:
The Python application path, which is the folder where you originally installed Python; and
The Python Scripts path. The Scripts folder should be located within the Python application path.
Here is how my Python application path looks like:
Python application path
And this is how my Python Scripts path looks like:
Python Scripts path
Now let’s fill the New User Variable box that you saw earlier:
New User Variable
For the Variable name, type ‘Path‘.
For the Variable value, copy the full Python application path, then use semicolon (as highlighted in yellow below), and finally copy the Python Scripts path.
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
Put all the values together in the New User Variable box:
New User Variable - Windows 10
Press ‘OK’ and you would then see your new Python Path under the ‘User variables’ section. Don’t forget to press ‘OK’ again so that the changes will get implemented.
How to add Python to Windows PATH
That’s it! You just added Python to the Windows Path.
You’ll now be able to install Python packages easily, by opening the Windows Command Prompt and then typing:
pip install package name
For example, to install the pandas package, simply type ‘pip install pandas’ and then press Enter:
How to add Python to Windows PATH
Similarly, you may upgrade PIP by typing the following command:
python -m pip install --upgrade pip

How to install Python (any version) in Windows when you've no admin privileges?

The "NO ADMIN PRIVILEGES" part is key. I need to install Python but I do not have access to it in order to run the installation in a proper way. I'm also behind a firewall, so the "pip" option is quite limited.
Could you help me figure this out?
From the Python website, download the MSI version of Python you wish to install.
Then open your command prompt and use this command:
msiexec /a python-2.7.10.msi /qb TARGETDIR=C:\python27
Substitute python-2.7.10.msi, if you downloaded Python 3 and adjust your TARGETDIR to where you want it to go.
The /qb flag will give you a small dialog progress bar.
I downloaded embeddable zip file from the site. Extracted it to the folder of my choice. Then added that folder in the windows path variable (using setx). It worked for me. Well this install only python and not the other packages like pip etc.
Later I found better and simpler way with Python 3.7.0 version for windows.
Download windows installer exe.
Run the exe.
Screen will be shown to chose the installation option.
Uncheck "install for all users" option.
Go for the custom installation.
On next screen specify the directory path for which your user have
full access on the computer.
Uncheck "create shortcuts for installed application" option.
Make sure "Add python to environment variable" option is Unchecked .
complete the installation.
Add the installation and Script folder path in PATH using setx
This has installed all the default components of python
As the comment states: "uncheck '[Install launcher] for all users'"..
I suggest you use Anaconda (open source edition).
Anaconda is a completely free Python distribution (including for
commercial use and redistribution). It includes more than 300 of the
most popular Python packages for science, math, engineering, and data
analysis.
One great feature of Anaconda is that it installs to your home folder, to which you should have write access.
It ships with a conda script that let's you install packages too.
Keep in mind that you will not be able to add it to the System PATH, though you can try to add it to your user PATH. If you can not add it to either, you will have to manually specify the full path to the python executable to run scripts from the command line.
I would suggest you try using Anaconda. It can be installed to a local user folder without requiring admin permissions.
With this, you also get a quite nice package management conda.
Uncheck 'Install for all user' when the installation starts. Rest will be taken care by Python-3.
I'm using Python-3.7.6 with this method.
This installation method will automatically updates current user's path for Python. But the application name will be py instead of python. The user has to handle environment variables if he wants to use pip or python commands.
As mentioned earlier here, use Anaconda. The default installation process does not require Windows Admin privileges. It comes with nice package management (that might not work if you are sitting behind corporate firewalls).
With Windows 2010 edition May 2019, you won't have to execute a msi setup or download an Anaconda edition or go to the python website.
Windows has a python.exe and python3.exe already pre-installed
those commands will get you directly to the Microsoft Store!
Example: Python 3.9, which installs python and puts python on the path for the current user. So, no need for admin privileges.
There is a discussion about this on the Talk Python To ME podcast #191 with Steve Dower from Dec. 2018.
See more at "Who put Python in the Windows 10 May 2019 Update?" from the same Steve Dower (also on Stack Overflow):
This version of Python is fully maintained by the community, installs easily on Windows 10, and automatically makes common commands such as python, pip and idle available (as well as equivalents with version numbers python3 and python3.7, for all the commands, just like on Linux).

Is it possible to easily extract python run configuration (with additional path) from Pycharm?

I have a working Python project on my PC, which I am running from Pycharm.
It uses Pyroot (an interface to Root C++ library), whose C++ lib path I have added in Project Settings/Python Interpreter/Paths in Pycharm. It also needs to use the 2.7 Python interpreter, instead of 3., which is a default python in my terminal.
I want to run this project remotely on another desktop, so I need to be able to run it from terminal specifying the path to Root and the interpreter version.
Is there a way to easily extract from Pycharm the exact run command it is using when I'm running the code via run button?
Alternatively, if that's impossible, how should I specify the path to Root and the interpreter version when running from terminal?
I guess to best way is to create a virtualenv either in the terminal or in pycharm including the corrext python version 2.7 and install pyroot via pip into this virtualenv. Then you can simply ssh in the remote host, activate the venv and start your project from the terminal. Or you ssh into it with X-forwarding and start Pycharm itself from your client.
If you select the correct project and go to File > Settings, under the Project Settings you can see the Project Interpreter which tells you which interpreter is being used.
Hope this is what you are looking for.

How to install both Python 2.x and Python 3.x in Windows

I do most of my programming in Python 3.x on Windows 7, but now I need to use the Python Imaging Library (PIL), ImageMagick, and wxPython, all of which require Python 2.x.
Can I have both Python 2.x and Python 3.x installed in Windows 7? When I run a script, how would I "choose" which version of Python should run it? Will the aforementioned programs be able to handle multiple versions of Python installed at once? I have searched for hours and hours for how to do this to no avail.
Thanks.
I found that the formal way to do this is as follows:
Just install two (or more, using their installers) versions of Python on Windows 7 (for me work with 3.3 and 2.7).
Follow the instuctions below, changing the parameters for your needs.
Create the following environment variable (to default on double click):
Name: PY_PYTHON
Value: 3
To launch a script in a particular interpreter, add the following shebang (beginning of script):
#! python2
To execute a script using a specific interpreter, use the following prompt command:
> py -2 MyScript.py
To launch a specific interpreter:
> py -2
To launch the default interpreter (defined by the PY_PYTHON variable):
> py
Resources
Documentation: Using Python on Windows
PEP 397 - Python launcher for Windows
What I did was download both 2.7.6 and 3.3.4. Python 3.3.4 has the option to add the path to it in the environment variable so that was done. So basically I just manually added Python 2.7.6.
How to...
Start > in the search type in environment select "Edit environment variables to your account"1
Scroll down to Path, select path, click edit.
Add C:\Python27;
so you should have paths to both versions of Python there, but if you don't this you can easily edit it so that you do..... C:\Python27;C:\Python33;
Navigate to the Python27 folder in C:\ and rename a copy of python.exe to python2.exe
Navigate to the Python34 folder in C:\ and rename a copy of python.exe to python3.exe
Test: open up commmand prompt and type python2 ....BOOM! Python 2.7.6. exit out.
Test: open up commmand prompt and type python3 ....BOOM! Python 3.4.3. exit out.
Note: (so as not to break pip commands in step 4 and 5, keep copy of python.exe in the same directory as the renamed file)
I have multiple versions in windows.
I just change the exe name of the version I'm not defaulting to.
python.exe --> python26.exe
pythonw.exe --> pythonw26.exe
As for package installers, most exe installers allow you to choose the python install to add the package too.
For manual installation check out the --prefix option to define where the package should be installed:
http://docs.python.org/install/index.html#alternate-installation-windows-the-prefix-scheme
If you use Anaconda Python, you can easily install various environments.
Say you had Anaconda Python 2.7 installed and you wanted a python 3.4 environment:
conda create -n py34 python=3.4 anaconda
Then to activate the environment:
activate py34
And to deactive:
deactivate py34
(With Linux, you should use source activate py34.)
Links:
Download Anaconda Python
Instructions for environments
To install and run any version of Python in the same system follow my guide below.
For example say you want to install Python 2.x and Python 3.x on the same Windows system.
Install both of their binary releases anywhere you want.
When prompted do not register their file extensions and
do not add them automatically to the PATH environment variable
Running simply the command python the executable that is first met in PATH will be chosen for launch. In other words, add the Python directories manually. The one you add first will be selected when you type python. Consecutive python programs (increasing order that their directories are placed in PATH) will be chosen like so:
py -2 for the second python
py -3 for the third python etc..
No matter the order of "pythons" you can:
run Python 2.x scripts using the command: py -2 (Python 3.x functionality) (ie. the first Python 2.x installation program found in your PATH will be selected)
run Python 3.x scripts using the command: or py -3 (ie. the first Python 3.x installation program found in your PATH will be selected)
In my example I have Python 2.7.14 installed first and Python 3.5.3. This is how my PATH variable starts with:
PATH=C:\Program Files\Microsoft MPI\Bin\;C:\Python27;C:\Program Files\Python_3.6\Scripts\;C:\Program Files\Python_3.6\;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared
...
Note that Python 2.7 is first and Python 3.5 second.
So running python command will launch python 2.7 (if Python 3.5 the same command would launch Python 3.5).
Running py -2 launches Python 2.7 (because it happens that the second Python is Python 3.5 which is incompatible with py -2).
Running py -3 launches Python 3.5 (because it's Python 3.x)
If you had another python later in your path you would launch like so: py -4. This may change if/when Python version 4 is released.
Now py -4 or py -5 etc. on my system outputs: Requested Python version (4) not installed or Requested Python version (5) not installed etc.
Hopefully this is clear enough.
Here's what you can do:
Install cmder.
Open and use Cmder as you would with you cmd terminal.
Use the command alias to create command aliases.
I did the following:
alias python2 = c:\python27\python.exe
alias python3 = c:\python34\python.exe
And that's it! ;-)
I actually just thought of an interesting solution. While Windows will not allow you to easily alias programs, you can instead create renamed batch files that will call the current program.
Instead of renaming the executable which will break a lot of thing including pip, create the file python2.bat in the same directory as the python2.exe. Then add the following line:
%~dp0python %*
What does this archaic syntax mean? Well, it's a batch script, (Windows version of bash). %~dp0 gets the current directory and %* will just pass all the arguments to python that were passed to the script.
Repeat for python3.bat
You can also do the same for pip and other utilities, just replace the word python in the file with pip or whathever the filename. The alias will be whatever the file is named.
Best of all, when added to the PATH, Windows ignores the extension so running
python3
Will launch the python3 version and and the command python2 will launch the python2 version.
BTW, this is the same technique Spyder uses to add itself to the path on Windows. :)
Starting version 3.3 Windows version has Python launcher, please take a look at section 3.4. Python Launcher for Windows
You can install multiple versions of Python one machine, and during setup, you can choose to have one of them associate itself with Python file extensions. If you install modules, there will be different setup packages for different versions, or you can choose which version you want to target. Since they generally install themselves into the site-packages directory of the interpreter version, there shouldn't be any conflicts (but I haven't tested this). To choose which version of python, you would have to manually specify the path to the interpreter if it is not the default one. As far as I know, they would share the same PATH and PYTHONPATH variables, which may be a problem.
Note: I run Windows XP. I have no idea if any of this changes for other versions, but I don't see any reason that it would.
What I have done on my own windows computer where I have Python 2.7 and Python 3.4 installed is I wrote a simple .bat file in the same directory as my Python.exe files. They look something like,
cmd /k "c:\python27\python.exe" %*
The %* allows you to add arguments (Python files) afterwards. I believe /k keeps the prompt open after it finishes running the script. Then I save that as python27.bat Then I go to my Python 3 directory and make a bat file there. Now in my command line I can write
Python27 helloworld.py
Or
Python34 helloworld.py
And they will run in their respective versions of Python. Make sure that c:\python27 and c:\python34 are in your environment variables.
I got my answer from here
I did this in three steps by following the instructions here: This is all taken directly from here: http://ipython.readthedocs.io/en/stable/install/kernel_install.html. I'm currently running Python 2.x on Windows 8 and have Anaconda 4.2.13 installed.
1) First install the latest version of python:
conda create -n python3 python=3 ipykernel
2) Next activate python3
activate python3
3) Install the kernel:
python -m ipykernel install --user
If you have Python 3 installed and want to install 2, switch the 2 and the 3 above. When you open a new notebook, you can now choose between Python 2 or 3.
Check your system environment variables after installing Python, python 3's directories should be first in your PATH variable, then python 2.
Whichever path variable matches first is the one Windows uses.
As always py -2 will launch python2 in this scenario.
I have encountered that problem myself and I made my launchers in a .bat so you could choose the version you want to launch.
The only problem is your .py must be in the python folder, but anyway here is the code:
For Python2
#echo off
title Python2 Launcher by KinDa
cls
echo Type the exact version of Python you use (eg. 23, 24, 25, 26)
set/p version=
cls
echo Type the file you want to launch without .py (eg. hello world, calculator)
set/p launch=
path = %PATH%;C:\Python%version%
cd C:\Python%version%
python %launch%.py
pause
For Python3
#echo off
title Python3 Launcher by KinDa
cls
echo Type the exact version of Python you use (eg. 31, 32, 33, 34)
set/p version=
cls
echo Type the file you want to launch without .py (eg. hello world, calculator)
set/p launch=
cls
set path = %PATH%:C:\Python%version%
cd C:\Python%version%
python %launch%.py
pause
Save them as .bat and follow the instructions inside.
Install the one you use most (3.3 in my case) over the top of the other. That'll force IDLE to use the one you want.
Alternatively (from the python3.3 README):
Installing multiple versions
On Unix and Mac systems if you intend to install multiple versions of Python
using the same installation prefix (--prefix argument to the configure script)
you must take care that your primary python executable is not overwritten by the
installation of a different version. All files and directories installed using
"make altinstall" contain the major and minor version and can thus live
side-by-side. "make install" also creates ${prefix}/bin/python3 which refers to
${prefix}/bin/pythonX.Y. If you intend to install multiple versions using the
same prefix you must decide which version (if any) is your "primary" version.
Install that version using "make install". Install all other versions using
"make altinstall".
For example, if you want to install Python 2.6, 2.7 and 3.3 with 2.7 being the
primary version, you would execute "make install" in your 2.7 build directory
and "make altinstall" in the others.
I just had to install them. Then I used the free (and portable) soft at http://defaultprogramseditor.com/ under "File type settings"/"Context menu"/search:"py", chose .py file and added an 'open' command for the 2 IDLE by copying the existant command named 'open with IDLE, changing names to IDLE 3.4.1/2.7.8, and remplacing the files numbers of their respective versions in the program path. Now I have just to right click the .py file and chose which IDLE I want to use. Can do the same with direct interpreters if you prefer.
I use a simple solution to switch from a version to another version of python, you can install all version you want. All you have to do is creating some variable environment.
In my case, I have installed python 2.7 and python 3.8.1, so I have created this environment variables:
PYTHON_HOME_2.7=<path_python_2.7>
PYTHON_HOME_3.8.1=<path_python_3.8.1>
PYTHON_HOME=%PYTHON_HOME_2.7%
then in my PATH environment variable I put only %PYTHON_HOME% and %PYTHON_HOME%\Scripts. In the example above I'm using the version 2.7, when I want to switch to the other version I have only to set the PYTHON_HOME=%PYTHON_HOME_3.8.1%.
I use this method to switch quickly from a version to another also for JAVA, MAVEN, GRADLE,ANT, and so on.
Only Works if your running your code in your Python IDE
I have both Python 2.7 and Python 3.3 installed on my windows operating system. If I try to launch a file, it will usually open up on the python 2.7 IDE. How I solved this issue, was when I choose to run my code on python 3.3, I open up python 3.3 IDLE(Python GUI), select file, open my file with the IDLE and save it. Then when I run my code, it runs to the IDLE that I currently opened it with. It works vice versa with 2.7.
I have installed both python 2.7.13 and python 3.6.1 on windows 10pro and I was getting the same "Fatal error" when I tried pip2 or pip3.
What I did to correct this was to go to the location of python.exe for python 2 and python 3 files and create a copy of each, I then renamed each copy to python2.exe and python3.exe depending on the python version in the installation folder. I therefore had in each python installation folder both a python.exe file and a python2.exe or python3.exe depending on the python version.
This resolved my problem when I typed either pip2 or pip3.
If you can't get anything else to work, open an interpreter in whichever version you choose (I prefer using iPython) and:
import subprocess
subprocess.call('python script.py -flags')
This uses whichever python version you are currently operating under. Works fine for a single script, but will quickly get out of hand if there are lots of scripts you run, in which case you can always make a batch file with all of these calls inside. Not the most elegant answer, but it works.
Is there a way to make aliases for different python version a la Linux?

Categories

Resources