I have set the PATH environment variable:
C:\Users\zy>path
PATH=e:\ProgramData\Anaconda3;e:\ProgramData\Anaconda3\Library\mingw-w64\bin;e:\
ProgramData\Anaconda3\Library\usr\bin;e:\ProgramData\Anaconda3\Library\bin;e:\Pr
ogramData\Anaconda3\Scripts;C:\Windows\system32;C:\Windows;C:\Windows\System32\W
bem;C:\Windows\System32\WindowsPowerShell\v1.0\;d:\Program
Files\Process Lasso\; ;e:\Program Files (x86)\Microsoft VS Code\bin
I can successfully call python by Win+R and cmd, then enter 'python'
but if I call cmd in a directory by shift+right click, it will suggest 'python is not recognized as an internal or external command'
so when I run bat file in a directory, it can not succeed.
#echo off
python
pause
Write this in your .bat file:
#echo off
start python
pause
When it comes to running applications with a batch file, you need to put the start keyword before writing in the applications path.
If you are still experiencing some trouble, then you should download python installer (corresponding to the version you are using right now) open it and you should see a choice called Modify. Click on it and make sure you check the box saying Add Python to environment variables.
Related
I'm migrating from Spyder to VSCode and would like to use it in a similar way.
One of the major problems I'm facing is to set de cwd to the folder of the file I'm working. There is a lot of question just like mine in the Google, but none of them solved my problem.
Here are my configs:
"code-runner.cwd": "${fileDirname}",
"code-runner.fileDirectoryAsCwd": true,
"terminal.integrated.cwd": "${fileDirname}",
"python.testing.cwd": "${fileDirname}",
"python.terminal.launchArgs": [
"-m",
"IPython"
],
"terminal.integrated.defaultProfile.windows": "Command Prompt",
"python.terminal.executeInFileDir": false
What I want to do is to execute my file in IPython with the cwd setted to my file directory, to load some CSVs that are in the file folder, also be able to run some independent lines or enter some code directly in Ipython with all the variables loaded.
I don't want to just RUN the file or DEBUG, because I want to prototype some lines of code right in the IPython console with all the variables loaded.
With my configs, what I usually do is to select all the lines of the script and CTRL + ENTER. This will open a terminal with Ipython but setted to my project folder, not my file folder.
If I open a REPL, I get the same problem: Ipython opened but not set to file folder. Worse I can't use the CTRL + Enter to send some independent lines to Ipython (in this situation CTRL + Enter will cause to open a new terminal)
If I open a terminal by the Terminal menu, the console is setted to my file folder, but without python.
The closest that I got is to open the terminal from Terminal menu, and in the terminal navigate to my python and execute IPython, but then I can't run some independent lines from my code using a shortcut (most precisely like the F9 in Spyder)
My Python is not in Windows path. I'm working with files from a disk E: and my python is on a disk C:.
No Work Reason:
"terminal.integrated.cwd": "${fileDirname}", is the configuration of the default terminal. You can find out the terminal name was powershell or something others. But when you take the command of Run Selection/Line in Python Terminal, the name of the terminal was the explicit name: Python.
Impossible Reason:
Unfortunately, the Python extension seems has not provided the configuration to modify the cwd when you take the command of Run Selection/Line in Python Terminal while you can modify the cwd when you take the command of Run Python File in Terminal. As the IPython has no configuration to modify the cwd too, so it seems has not a suitable method to achieve your aim.
Workaround:
Method1: After you into the REPL mode, modify the cwd like this:
import os
os.chdir('/tmp')
Method2:
Change the configuration: "python.terminal.executeInFileDir": true
Execute the command of Run Python File in Terminal first, in order to change the terminal path.
Execute the command of Run Selection/Line in Python Terminal.
Method3:
Execute the command of Run Selection/Line in Python Terminal.
Exit out and change the path of the terminal.
Run IPython again.
I would like to run my python script by a custom command
Previously, we would have to run
python hello.py
For example, I would type:
hello
in my command prompt
then my computer will run my hello.py
You can create a simple batch file and place it in a folder that is in your system path variable.
Name it $Py.bat
#Echo off
If not "%~1" == "" (
python "%~1.py" 2> nul || (Echo/"%~1.py" failed to execute correctly or was not found.)
) Else Echo/Filename required.
Then run it in cmd.exe using:
$Py hello
Another approach would to be to create a macro using setx
SetX run "python F.py"
SetX adds the variable with it's value to the registry. It is not available in the Session in which it is created, nor in any child environment of that Session.
Execute by expanding the variable with substring modification replacing F with the filename:
%run:F=hello%
There are two pieces to this. The first is the PATHEXT environment variable that stores a list of the file extensions for operation system to execute. When running a command line that does not contain an extension, the system uses the value of this environment variable to determine which extensions to look for and in what order, such as .com first, follow by .exe, .bat, .cmd, which happens to be the default value stored in the PathExt by Windows. On my computer:
C:\>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py;.pyw
If .py is not part of your PATHEXT environment variable, you can add it by right-mouse clicking on the Start box in the lower left corner of your screen and the clicking on Settings. When the new window comes up, type environment in the search box and then click on one of the Edit choices:
This now allows you to type in hello instead of hello.py at the command prompt. Referring back to my environment variable, if there were two files hello.bat and hello.py the system would choose hello.bat because the .BAT extension is listed before the .py extension (everything is case insensitive). But the second piece of the puzzle is associating the Python interpreter with the .py extension so that the system knows how to run that type of file. Enter the Windows ftype command. This produces a list of mappings of file type extensions and the programs used to launch those extensions. On my computer, one of the lines listed is:
Python.CompiledFile="C:\WINDOWS\py.exe" "%L" %*
I installed Python using a standard installation from python.org and when I did I installed the optional Python launcher for Windows, which is the "C:\WINDOWS\py.exe" you see above. This launcher supports multiple Python versions and allows you to control which Python version will execute your Python program based on the shebang that you code as the first line of your program. But generally, when you install Python under Windows, an association is made between the .py file type and the Python interpreter. If you do not have this association, you can look for your Python file in an Explorer window, right-mouse click on it and then choose Open With:
My options show Python since I already have the association. In your case it would probably be missing, so you would pick "Choose Another App" and then if you still don't see Python in the list you would choose "Look for another app on this PC" and then navigate to the Python interpreter. Either way, be sure to click on the box labeled "Always use this app to open .py files":
I am completely new at jenkins. I installed the windows package and tried it out but I am a little lost.
Here is my issue. I basically need jenkins to run a python script located on my computer. It's a scipt that has 1 line
print("hello World")
I created a new build setup with the following under the Build Environment
"Execute windows batch command"
cd "C:\perforce\projects\DEV\E9318_SuperNova\P22_module_split_up\Ref\Tools\"
"C:\perforce\projects\DEV\E9318_SuperNova\P22_module_split_up\Ref\Tools\test.py"
The console output from the build
Building in workspace C:\Program Files (x86)\Jenkins\workspace\E9318_SuperNova-P22_Compensator_FPGA_PSA
[E9318_SuperNova-P22_Compensator_FPGA_PSA] $ cmd /c call C:\windows\TEMP\jenkins7605841087640242580.bat
C:\Program Files (x86)\Jenkins\workspace\E9318_SuperNova-P22_Compensator_FPGA_PSA>cd "C:\perforce\projects\DEV\E9318_SuperNova\P22_module_split_up\Ref\Tools\"
C:\perforce\projects\DEV\E9318_SuperNova\P22_module_split_up\Ref\Tools>"C:\perforce\projects\DEV\E9318_SuperNova\P22_module_split_up\Ref\Tools\test.py"
C:\perforce\projects\DEV\E9318_SuperNova\P22_module_split_up\Ref\Tools>exit 103
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
I am not sure what I am doing wrong. Any help would be highly appreciated,
Thanks,
Amish
*.py files don't seem to be executable directly (as invoked from a shell)
Change the second line of your script with :
"C:\path\to\python.exe" "C:\perforce\projects\DEV\E9318_SuperNova\P22_module_split_up\Ref\Tools\test.py"
Anyway, this form field is expecting the path to a script that makes the build and runs the tests.
I will also refer to
How to set the PATH environment variable in Jenkins configuration on Windows? as a possible solution. After I added the python executable path to the node's path, I could call my script using
python.exe "C:\perforce\projects\DEV\E9318_SuperNova\P22_module_split_up\Ref\Tools\test.py"
I just downloaded and installed Anaconda on my Windows computer. However, I am having trouble executing .py files using the command prompt. How can I get my computer to understand that the python.exe application is in the Anaconda folder so it can execute my .py files?
You should use Anaconda Prompt instead of common Windows command prompt.
Then navigate to your folder with the .py file and run:
python myfile.py
However if you want to use normal command prompt you should put the path with you're python.exe which is usually in
C:\Users\<username>\AppData\Local\Continuum\anaconda3\python.exe
behind this one put your .py file.
Launch JupyterLab from Anaconda
(Perform the following operation with JupyterLab ...)
Click on icon folder in side menu
Start up "Text File"
Rename untitle.txt to untitle.py (The name of the file started up was also changed)
Start up the "terminal" (In windows the power shell starts up)
Execute the command python untitle.py
Right click on a .py file and choose 'open with'
Scroll down through the list of applications and click something like 'use a different program'
Naviage to C:\Users\<username>\AppData\Local\Continuum\anaconda3
click on python.exe and then click on 'ok' or 'open'
Now when you double click on any .py file it will run it through Anaconda's interpreter and therefore run the python code.
I presume if you run it through the command line the same would apply but perhaps someone could correct me?
Just get to the home of jupyter notebook and select "New" then select "Text file".
Then save the text file as file_name.py
Write your code in the file and save the file.
Then open the "Anaconda Prompt" and then type as follows to run your file
python file_name.py
You can do it from the "Anaconda Prompt"
conda run "my_script.py"
I was doing exactly as Martin Bosch suggested, and was getting the following:
(base) C:\>python command.py
python: can't open file 'command.py': [Errno 2] No such file or directory
I solved it this way:
navigate to the exact file location using the "cd" command
for me this was:
(base) C:\>cd my_scripts
this should put you specifically in the file where your .py script is located.
now you should try to input the name of your file.
(base) C:\my_scripts> test_script.py
you may get asked which program to run this with, and simply find python.exe
After doing this process once, I can simply type (in anaconda prompt)
test_script.py
and it runs no problem, even from the top of the file tree (I don't have to be in the exact file, nor do I have to explicitly give the whole file path)
Anaconda should add itself to the PATH variable so you can start any .py file with "python yourpythonfile.py" and it should work from any folder.
Alternatively download pycharm community edition, open your python file there and run it. Make sure to have python.exe added as interpreter in the settings.
If you get the following error:
can't open file 'command.py': [Errno 2] No such file or directory
Then follow this steps to fix it:
Check that you are in the correct directory where the Python file is.
If you are not in the correct directory, then change the current working directory with cd path. For instance: cd F:\COURSE\Files.
Now that you are in the directory where your .py file is, run it with the command python app.py.
Check where is the directory for the ananconda environment directory which is generally
"C:\Users\[UserName]\.conda\envs\[conda environment directory]"
You will see python.exe in that directory.
After that, you need to use the following command to execute your python file (i.e. xx.py) when you are running Anaconda prompt and you will be done:
"C:\Users\[UserName]\.conda\envs\[conda environment directory]\python.exe" xxx.py
BTW, if you have global variable (i.g. variable yyy) that contain directory, you have to define the global variable that contains full path of directory just below the header (the import section) to prevent the "name 'yyy' is not defined" error to occur:
from pathlib import Path # dealing with path issue
yyy = Path("[DriverLettter]:\Full\Path\of\Directory")
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