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")
Related
I have my first ever hello_world.py file saved in documents\python_work. It runs fine in the Sublime text editor but when I try to navigate to it through a command prompt window, it can't seem to find it. I open a command prompt and type cd documents and it works. I type dir and it shows me the files, but when I type cd python_work (which is the folder my file is in) I get:
The system cannot find the path specified.
The textbook had me add C:\Python and C:\Python\Scripts to the PATH environment variables (not too sure why, just following directions), so perhaps I made a mistake during this process?
If you are the the same folder as the file, type python(or py, depending on your python version) python file.py to run it.
A quick way to get to the folder is find it in the file explorer, and where it shows the file path, click there and type 'cmd' and hit enter. It will open up the Command Prompt from that folder so you don't have to manually navigate to it.
You can go to the File Explorer, right-click on your hello_world.py file and then:
Properties > Details > File Path
and then proceed to copy that path in your cmd.
How to open my .py script from anywhere on my pc directly into python idle form command prompt?
Is there any way so that i can typeidle test.py on cmd so that it opens the test.py file in the current directory and if test.py is not available creates a new file and opens it into idle
You can do that by adding the directory where you have installed the idle editor the PATH environment variable.
How to do that depends on your operating system: just search the Internet for add directory to path plus your operating system: e.g. Windows/Ubuntu, etc.
After changing the environment variable it may be a good idea to restart your PC (to make sure that all programs use the updated version)
You can right click on the idle then you will can see location of it in the properties tab
Navigate to that location and call as below
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 2.7>"IDLE (Python GUI).lnk" C:\Python27\projects\ping.py
Create a idle.bat file containing
#echo off
{Python path}\Lib\idlelib\idle "%cd%\%1"
Where {Python path} should be replaced with the directory where python is installed.
Example:-
#echo off
C:\Python36\Lib\idlelib\idle "%cd%\%1"
Copy this idle.bat file into the python directory i.e {Python path} (C:\Python36 in case of the example)
Make sure you've add python path to environment variables
Open cmd in your folder then type
idle test.py
I am currently reading the book Learn Python the Hard Way by Zed Shaw. On exercise 1 after learning the basics of Powershell we must open a notepad++ saved .py file in Powershell. Now here is the problem I'm having.
I am supposed to open this python file by running this command:
PS C:\Users\Trevor> python ex1.py
Zed Shaw does this in his book and it prints back what the file contains...("hello world")
Now i understand my path is wrong by the error message i receive telling me that python is not recognized. I have tried many many times to get the correct path to open python. I have saved the python27 file to my desktop and just about everything trying to get a path that will work.
I am starting at:
PS C:\Users\Trevor>
Any idea how to get to my python27 file and open python when it is saved to my desktop? I also have my ex1.py file saved to my python27 folder. Should i remove it? please help if you can thanks!
PowerShell cannot find python in the current directory or in the directories specified in PATH environment variable.
You can add your installed python directory to PATH variable in System Properties (Win+R → type in sysdm.cpl) → Advanced → Environment variables... → Under "user variables for ...", find PATH variable (if there aren't any, create it using New...), double click to edit it and insert "<your python path>;" (without the quotes). After that, restart PowerShell, run your command and you're done.
I also had this problem, but for me the solution to it wasn't creating a path. I typed in the same code as you, and this is the error message I got:
C:\Users\UserName\AppData\Local\Programs\Python\Python36\python.exe: can't open file 'ex1.py': [Errno 2] No such file or directory
The fix to this was a lot easier than I expected it to be, but it took me a little bit longer to figure out what it was since I just started learning how to work with Python and PowerShell.
This is what you need to do to solve this issue:
Instead of typing the name of the file you are trying to open, you first need to open the directory (the folder where all of your python files are stored).
To open the directory, type in cd DirectoryName and then press enter.
After that, type in "python" (without the quotes) and then the file name (for example, ex1.py). Then, press enter.
At this point, PowerShell should be able to open the file.
This is what you should see:
PS C:\Users\UserName> cd DirectoryName
PS C:\Users\UserName\DirectoryName> python FileName.py
(Note: This is when the file would print)
If your file still isn't opening, you may need to be more specific about the path to the directory. I have my directory located in C:\Users\UserName. If your directory is in C:\Users\UserName but you have it embedded within another folder, you may have to open that folder in PowerShell (you can do this by typing cd FolderName) before you can open the directory folder that's inside it. The easiest thing to do is to save your directory folder in the path C:\Users\UserName so that opening files will take less time/typing.
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
When attempting to run .py files from the command prompt I get this,
C:\users\ocean>python helloworld.py
python: can't open file 'helloworld.py': [Errno 2] No such file or directory
The environment variables are already set to ;C\python27 and ;C\python32 (yes, I'm running both releases of python.)
Do I have to set a new variable? What must I do? Running Windows 7, it was a pain to get it to even run that error, before 'python' wasn't even recognized.
Edit: C:\users\oceans does not include my python folder. Is there anyway to redirect the directory so that I won't have to move all of the python files to ocean?
You have to either do:
python c:\path\to\the\file.py
or
cd c:\path\to\the
python file.py