Unable to get a command executed in python - python

I am unable to run the following command
C:\Users\deepa>python ..\scrape.py
It gives me an error
python: can't open file 'C:\Users\scrape.py': [Errno 2] No such file or directory
I have already set environment variables to get python executed from any directory.

If you have set your environment variables then you don't need to pass the path of the file (which you are passing is incorrect) while running the code. Just you can mention file name and it will get executed.
python scrape.py

For your command:
C:\Users\deepa>python ..\scrape.py
The problem is not related to the python path, it's about the ..\scrape.py,
Please make sure this python script (scrape.py) is in the right place, I recommend you use absolute path instead of related path, for example:
C:\Users\deepa>python D:\test\test.py
D:\test\test.py is the absolute path of this python script (scrape.py), or you can drag this file into your terminal/cmd/PowerShell etc., you will see its absolute path.

Related

Relative File Path is not recognized by Python in VSCode

When I am using Absolute path the code is working fine but using relative path throwing FileNotFoundError in python.
f = open("Input.txt","r")
Your python file is executed by the terminal. You can clearly see that your terminal is at the folder ...Desktop\cs\Python\myproject\. Since the file "Input.txt" does not exist relative to the path of your terminal, you are getting this error. (That is, the path ...Desktop\cs\Python\myproject\Input.txt does not exist)
A simple solution would be to use absolute path in your python file instead of the relative path.
Another cheap solution is to use the terminal, go to the correct folder and run your file, as intended by God.
If you really want to dedicate a single button for running, you can try the following:
EDIT: Okay, I understand you are using the "Run button" at top of python files to run.
You only need to set the setting python.terminal.executeInFileDir to true.
In Settings, search for python.terminal.executeInFileDir and mark it. That should be what you need.
A quick solution to use relative paths can be to right click on the file, copy relative path and replace "" with "/". You can do it manually or with the function .replace("","/").
"route\input.txt".replace("","/")

How to fix "IOError: [Errno 2] No such file or directory" when making Virtual Environments

Whenever I try to use the 'virtualenv VirtualEnvironmentName' command or the 'virtualenv -p python3.8 VirtualEnvironmentName' command it says "IOError: [Errno 2] No such file or directory." I just want to make Virtual Environments, but I always get that error saying "No such file or directory."
Thanks in advance.
To create a virtual environment, you must specify a path.
Then you can activate the python environment by running the following command:
your_working_directory\\Scripts\\activate
Most likely, the problem is that you're using a relative path for the directory.
Let me clarify how Python finds files:
An absolute path is a path that starts with your computer's root
directory, for example 'C:\Python\scripts..' if you're on Windows.
A relative path is a path that does not start with your computer's root
directory, and is instead relative to something called the working
directory. You can view Python's current working directory by calling
os.getcwd().
Other common mistakes that could cause a "file or directory not found" error include:
You may be using escape sequences in a file path:
path = 'C:\Users\apps'
Incorrect! The '\n' in 'Users\apps' is a line break character!
To avoid making this mistake, you can use any one of the below methods:
use raw string literals
path = r'C:\Users\apps'
you can always use this:
'C:/Users/apps'
another possibility is:
'C:\\Users\\apps
if it keeps answering something like this
Try to uninstall and reinstall Anaconda, but now checking the box below
When creating the virtual environment, I was getting a error similar error.
I resolved it by removing anaconda from the PATH, and then adding the actual python dir.

Running Python scripts through the Windows Command Line

I've just started learning Python using Learning Python by Mark Luts. In his book he offers an example of a simple script that is called through the Windows shell. In the example, he calls is as follows:
C:\code> python script1.py
I've gone and modified the Environment Variables on my machine so that I can call
C:\User\Example> python
to open up the interpreter and I can also call something like
C:\User\Example> script1
to run a script that I've written and placed in my desired directory. My issue is that I can not call
C:\User\Example> python script1.py
in my command line the same way he does in the book. He's mentioned something about a PYTHONPATH Environment Variable, however, this variable isn't present on my machine. I only have 'path', 'TEMP', and 'TMP'. Particulary, when I try to make such a call I get the error
python: can't open file 'script1.py': [Errno 2] No such file or directory
What do I have to do in order to get this sort of command to work properly on the command line?
From the book (p. 44, 4th Ed):
Finally, remember to give the full path to your script if it lives in a different directory from the one in which you are working.
For your situation, this means using
C:\User\Example> python C:\User\Example\my_scripts\script1.py
You could write a batch file that looks for the script in a predefined directory:
#echo off
setlocal
PATH=C:\User\Example\Python36;%PATH%
SCRIPT_DIR=C:\User\Example\my_scripts
python %SCRIPT_DIR\%*
You are calling python from within the context of C:\User\Example, and passing it a name of a file you want to run through the intepreter (script1.py). It is clear that the PATH variable is setup correctly such that you can call python from anywhere on you computer, since we can see that it is running but not actually able to find your script.
However, you stated in the comment that your scripts are actually located in C:\User\Example\my_scripts. In other words, you are passing python a name of a file that doesn't exist!! (at least from the contect of C:\User\Example).
You need to be in the directory of the script in order for the python executable to be able to find it.
Alternatively, you can run the python command and give it more information as to where the script is. For instance, you could run python .\my_scripts\script1.py if you are running from within the contect of C:\User\Example and your scripts are in C:\User\Example\my_scripts.

Using only relative path instead of full path

I'm a very new python user using python 2.6.2 and my question is simple.
I want to only have the relative path "\file_name" in an input file instead of the full path like "c:\folder_a\folder_b\file_name" but when I use the relevant path in my input files I get the error "Windows Error [Error 2]: The system cannot find the file specified..." otherwise my code works fine.
What do I need to do/change so the system can use the relative path? It seems since I'm running the script from the same folder such as "c:\folder_a\folder_b>python script_name" in the command terminal the relevant path alone should work.
Just try '.\file_name' as your path
2 issues . = current directory, (.. is up one), and you need to escape the \ as \ if using windows file separators.

script file not found when using cmd.exe

I'm just getting started with Python and am trying to run a program from the command line, as it is done on this website under the heading "Python Program". So I made script hello.py, it's located in my computer at C:\Python27.
In the example, they run the script by typing python hello.py Guido. When I try to do this, it doesn't work. Firstly, I'm not entirely sure what is meant by the 'command line', but I'm using cmd.exe in Windows XP. I get this:
python: can't open file 'hello.py': [Errno 2] No such file or directory.
I have already specified PATH as C:\Python27.
Also, when I try to run the program from the Python shell by typing hello.py Guido I get
SyntaxError: invalid syntax.
When you start cmd.exe, the default directory is your Documents and Settings: since your file hello.py is not there, the python interpreter can't find it, thus giving you the [Errno 2] No such file or directory error. To solve that, just change your current working directory:
C:\Documents...>cd C:\Python27
C:\Python27> python hello.py Guido
Anyway, it is a good approach not to having your files inside the python directory (create a directory in your documents for python sources and use the same approach).
When you are running the python shell, you cannot explicitly call python files, so in your case it tries to run hello.py as a command (which doesn't exists) and it gives you a syntax error.
You need to locate your cmd current directory at C:\Python27:
cd C:\Python27
because the path python loads is relative. You can also use a full path:
python C:\Python2.7\hello.py
Try without "python", when you put python directory in path, it automatically connects ".py" extension with python, so there is no need in writing "python hello.py Guido"
Just go to directory where .py is located, and call "hello.py"
What's your current working directory and where is hello.py located? To execute that command, hello.py should be in the same directory from where you started the commend line (cmd.exe). Otherwise you need to the write the absolute path of hello.py (like python C:.....\hello.py Guido) instead of just the filename 'hello.py'.
I had also this problem but because of ether reason: I accidently added spaces to the names of some of the file's names, so the CMD didn't recognized the names.
for example: 'run .bat'

Categories