Run python scripts in windows - python

I have installed Anaconda on my Windows 10 to install Python 3.6.
I now want to run some Python scripts like test.py.
As far as I understood, I need to initialize some system variables, but I am not really able to get the whole meaning. I can set system variables by typing sysdm.cpl at the start button. Is this the right way? Otherwise?

If you want to set env variables permanently, then yes, this is the right way (start->sysdm.cpl->advanced->Environment variables)
If you want to set some variables for just one particular session, then open cmd.exe and set variables like
set _variable_=_value_
and then run your script.

1- open cmd
2-use cd to go to the file where your script is located(cd.. to go to previous folder and cd name to go to another folder)
3-when you get there in cmd type
4-python filename.py
if you are using anaconda instead of cmd use anaconda prompt
adding python to path:
1-open windows explorer
2-on the left side left click this pc and choose properties
3-then go to advanced system settings
4-then at the bottom enviorment variables
5-if you are windows 10 there will be onedrive written at top and behind it path choose path
6-then click new
7-add python directory to the path and then python directory/scripts
https://www.youtube.com/watch?v=Y2q_b4ugPWk check this

Related

Is it possible to handle the Environmental Variables within the Batch Script?

I created a very simple batch file as a launcher for a python script, however it does not work fine depending on the "PATH" setup for the user (Python 2 x Python 3).
Since changing the PATH can bring issues with other Python 2 based applications , could I make any update in the .bat to make a temporary change in the PATH while the Py script is running?
The idea is to use the batch to have the minimum interference from the user in the system setup (preferable a double click only).
In case of yes, what could I do?
#echo off
title ###Beta Script Launcher###
python myscript.py
pause
To make a temporary change to the path variable:
path c:\mydir;%path%
the documentation
c:\srv> path /?
Displays or sets a search path for executable files.
PATH [[drive:]path[;...][;%PATH%]
PATH ;
Type PATH ; to clear all search-path settings and direct cmd.exe to search
only in the current directory.
Type PATH without parameters to display the current path.
Including %PATH% in the new path setting causes the old path to be
appended to the new setting.
the normal way to do this for python, however, is to create a virtualenv (https://pypi.org/project/virtualenvwrapper-win/)
mkvirtualenv myenv
.. install everything in this environment ..
and then use it in your start script
#echo off
title ###Beta Script Launcher###
call workon myenv
python myscript.py
pause
(disclaimer, I'm one of the maintainers of virtualenvwrapper-win)
Yes you can edit the environment variables in batch script by using the following command
setx path "%PATH%;C:\New Folder"
based on wherever your python that you want to use is installed.
But this will change the path permanently

Using python with Anaconda in Windows

I have just installed Anaconda on my computer because I need to use Numpy.
Well, when I use python I for some reason have to be in the same folder as python.exe and, of course, now that I want to use Anaconda I have to be in the Anaconda3\Scripts folder where python.exe isn't. This is a nightmare, how can I use anaconda with python on a windows computer? Why does it have to be so complicated?
I think you are referring to the command-line use of python?
If you have admin priviliges on your machine you can add python to your environment variables, making it available in the console anywhere. (Sorry for different spellings, I am not on an english machine)
Press Shift+Pause ("System")
Click "Advanced System Options"
Click "Environment variables"
In the lower field with "System variables" there is a variable called PATH. Append the complete path to your python.exe without the file to that by adding a ; behind the last path in the variable and then adding your path. Do not add any spaces!
Example: C:\examplepath\;C:\Python27\
When you install anaconda on windows now, it doesn't automatically add Python or Conda to your path.
If you don’t know where your conda and/or python is, you type the following commands into your anaconda prompt (it comes when you install anaconda)
Next, you can add Python and Conda to your path by using the setx command in your command prompt.
Next close that command prompt and open a new one. You should now be able to use the python command. To do this you open a command prompt and type
python nameofPythonfile.py
Source: https://medium.com/#GalarnykMichael/install-python-on-windows-anaconda-c63c7c3d1444
To be able to do that in the command line you just have to add Python and also the Anaconda3\Scripts directory to your system path.
Here is a good tutorial on setting your path in Windows:
http://www.computerhope.com/issues/ch000549.htm

Python Script Will Not Run In Sublime Text 3 [duplicate]

I just downloaded and installed Python 2.7.2 to my laptop and I am trying to run some sample programs. My laptop is running Windows XP.
When I open a cmd window and type python I get:
'python' is not recognized as an internal or external command, operable program or batch file.
I am not a Windows person (mostly mainframes). However I am guessing that there is some command that I need to execute to define python as a command. Can anyone tell me what that command is?
The installer placed Python at C:\Python27\.
You need to add the python executable path to your Window's PATH variable.
From the desktop, right-click My Computer and click Properties.
In the System Properties window, click on the Advanced tab.
In the Advanced section, click the Environment Variables button.
Highlight the Path variable in the Systems Variable section and click the Edit
button.
Add the path of your python executable(c:\Python27\). Each different directory is separated with a
semicolon. (Note: do not put spaces between elements in the PATH. Your addition to the PATH should read ;c:\Python27 NOT ; C\Python27)
Apply the changes. You might need to restart your system, though simply restarting cmd.exe should be sufficient.
Launch cmd and try again. It should work.
This is because the Python exec are not in the search path of your operating system. In windows, start CMD. Type in
setx PATH PythonPath
where PythonPath is usually C:\Python27 or C:\Python33 or C:\Users\<Your User Name>\AppData\Local\Programs\Python\Python37 depending on your Python version. After restarting the CMD, you should get see outcomes when typing
Python --version
Python comes with a small utility that fixes this. From the command line run:
c:\python27\tools\scripts\win_add2path.py
Make sure you close the command window (with exit or the close button) and open it again.
Just another clarification for those starting out. When you add C:\PythonXX to your path, make sure there are NO SPACES between variables e.g.
This:
SomeOtherDirectory;C:\Python27
Not this:
SomeOtherDirectory; C:\Python27
That took me a good 15 minutes of headache to figure out (I'm on windows 7, might be OS dependent). Happy coding.
I had the same problem for a long time.
I just managed to resolve it.
So, you need to select your Path, like the others said above.
What I did:
Open a command window. Write set path=C:\Python24 (put the location and the version for your python). Now type python, It should work.
The annoying part with this is that you have to type it every time you open the CMD.
I tried to do the permanent one (with the changes in the Environmental variables) but for me its not working.
You can do it in python installer:
Go to Control Panel / System / "Advanced" tab / Enviromental Variables
Find variable called PATH in the lower list, and edit it. Add to the end C:\Python27
Open a new cmd window and try now.
emphasis: Remember to always RESTART the CMD WINDOW after setting the PATH environmental variable for it to take effect!
in PowerShell enter this:
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27",
"User")
Close PowerShell and then start it again to make sure Python now runs. If it doesn’t,
restart may be required.
Further to #Udi post this is what the script tried to do, but did not work with me.
I had to the set the following in the PATH nothing else.
C:\Users\hUTBER\AppData\Local\Programs\Python\Python35
C:\Users\hUTBER\AppData\Local\Programs\Python\Python35\Scripts
Were mine and now python works in the cmd
Easy. Won't need to get confused but paths and variables and what to click. Just follow my steps:
Go to the python installer.
Run it.
Out of the 3 options choose modify.
Check py launcher.
Next.
Check "Add python to environment variables"
Install.
Restart the cmd when finished and boom done
If you are trying to install python version python-3.9.6 then click the checkbox of Add Python 3.9 to PATH
Make sure you click on Add python.exe to path during install, and select:
"Will be installed on local hard drive"
It fixed my problem, hope it helps...
Another helpful but simple solution might be restarting your computer after doing the download if Python is in the PATH variable. This has been a mistake I usually make when downloading Python onto a new machine.
After restarting my machine then Windows will often recognize Python in the PATH variable.

How to run a Python project?

I have installed the Enthought Python distribution on my computer, but I don't have any idea how to use it. I have PyLab and IDLE but I want to run .py files by typing the following command:
python fileName.py
I don't know where to write this command: IDLE, PyLab or Python.exe or Windows command prompt. When I do this in IDLE it says:
SyntaxError: invalid syntax
Please help me to figure this out.
Open a command prompt: Press ⊞ Win and R at the same time, then type in cmd and press ↵ Enter
Navigate to the folder where you have the ".py" file (use cd .. to go one folder back or cd folderName to enter folderName)
Then type in python filename.py
Indeed, the command to run a Python file should be run in the command prompt. Python should be in your path variable for it to work flexible.
When the python folder is added to path you can call python everywhere in the command prompt, otherwise just in your python install folder.
The following is from the python website:
Windows has a built-in dialog for changing environment variables
(following guide applies to XP classical view): Right-click the icon
for your machine (usually located on your Desktop and called “My
Computer”) and choose Properties there. Then, open the Advanced tab
and click the Environment Variables button.
In short, your path is:
My Computer ‣ Properties ‣ Advanced ‣ Environment Variables In this
dialog, you can add or modify User and System variables. To change
System variables, you need non-restricted access to your machine (i.e.
Administrator rights).
Another way of adding variables to your environment is using the set
command in a command prompt:
set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
If you do it via My Computer, then look for the line named path in Enviroment Variables. Give that the value of your Python installation folder.
Set PYTHON variable to point to the full path of python.exe.
Then type in command prompt console window:
C:\path_to_folder\ python filename.py

how to add python to system dependency on win7?

i've got a problem when i'm doing dev
I managed to use
python manage.py runserver in a CMD shell
but the system cant find python
How could I add python to the system dependency to make the commandline work?
There are two basic ways you can do this in Windows.
Setting the PATH in the cmd shell
The first way is only local to the CMD shell you are currently in, and will have to be done again if you opened a new shell.
You can set your PATH to include the directory where python.exe is located.
In your CMD shell you can do:
set PATH=%PATH%;C:\path\to\python\install
So if Python was installed in C:\Python27, you would do this:
set PATH=%PATH%;C:\Python27
Setting the environment for your user throughout Windows
Alternatively, you can set your PATH permanently by changing the environment variable in Windows. Setting this will affect the rest of your Windows environment.
Right click "My Computer"
Select "Properties"
Click the "Advanced" tab in the new window.
Click on the "Environment Variables" button.
Edit the variable named PATH
Information about doing the latter at Microsoft: http://support.microsoft.com/kb/310519

Categories

Resources