I want to open python shell in the current directory like cmd. My system is windows 10. How can I achieve that?
Something like that:
Suppose that I open a directory in D disk, let's say the path is "D:\PythonProjects". I tried to open cmd in current directory, in cmd I type
"python" to get python shell, but the work directory of python didn't chage.
Did you try something like this in command line?
C:> D:
D:> cd PythonProjects
D:\PythonProjects> python
[Something about Python interpreter]
>>>
You can just simply open shell
and than
import os
os.chdir(" ")
In between quotes you can specify path were you want to open that
You can also see current path by
os.getcwd()
Related
So when I try to find the path from the cmd with where azuredatastudio, I get the path. When I go in Python and do print(os.environ), I get many defined paths, but not this from the upper command in cmd.
How to get in this example azuredatastudio path from Python and where is it stored?
The WHERE command is roughly equivalent to the UNIX 'which' command. By default, the search is done in the current directory and in the PATH.
Source: https://ss64.com/nt/where.html
So you'll have to explicitly look at the paths in the PATH environment variable: os.environ['PATH']. You'll find an implementation in this question here for example: Test if executable exists in Python?
Also, you should be able to just run the command from Python:
from subprocess import check_output
path = check_output(["where", "azuredatastudio"])
print(path)
A easy way to do this is:
import os
os.system("where azuredatastudio")
or if you want to save it in a variable.
import subprocess
process = subprocess.Popen("where azuredatastudio",stdout=subprocess.PIPE)
print(process.stdout.readline())
When i run in terminal i get the [Errno 2] No such file or directory error, but when i use debug mode it works.
the error occurs here,
list = open(filename, "r")
the code also works with IDLE
Your debugger is most likely running in a different location than your terminal. cd your terminal to where the file is located, or try using the absolute path instead of a relative one.
If you add these few lines in your code and run it using your IDE or Terminal, you'll notice the difference:
import os
curdir = os.getcwd()
print('My current directory is {}'.format(curdir))
fullpath_to_filename = os.path.join(curdir, filename)
print('When I run `open(filename)`, python sees: {}'.format(fullpath_to_filename))
print('This filepath is {}valid'.format('' if os.path.exists(fullpath_to_filename) else 'not '))
You'll likely experience something similar to below:
# on IDE:
My current directory is c:\Users\me\Projects\
When I run `open(filename)`, python sees: c:\Users\me\Projects\test.txt
This filepath is valid
# on Terminal:
My current directory is c:\Programs\Python38\
When I run `open(filename)`, python sees: c:\Programs\Python38\test.txt
This filepath is not valid
Solution: use absolute path for your filename, or os.chdir(...) to the correct parent path before location filename. I would recommend the former to avoid managing directories.
I encountered such a weird situation:
naivechou#naivechou/~>python test.py
test
naivechou#naivechou/~>pwd
/home/naivechou
naivechou#naivechou/~>python /home/naivechou/test.py
C:\toolchain\python\python.exe: can't open file '/home/naivechou/test.py': [Errno 2] No such file or directory
My working directory is /home/naivechou/, test.py is in there.
If I run test.py with absolute path, I'll get an error message of No such file or directory.But everything will be fine if I enter that directory and then run it. What's wrong with python?
Try moving into the folder where the python script is located and do a "ls" command there in Linux. if windows then do 'dir'. if you see the required file there then execute the following command
C:\location_where_the_script_is> python yourfile.py
For commands entered on the command line, Windows doesn't recognize forward-slashes as directory separators.
Your second example is looking in the current directory for the literal filename /home/naivechou/test.py, and of course such a filename does not exist.
Use backslashes instead, as is the Windows way:
python \home\naivechou\test.py
I am trying to run a python script from the windows command prompt, but I receive the following error message:
"python: can't open file 'pacman.py': [Errno 2] No such file or directory"
when I try the command:
c:\Program Files (x86)\Python27>python pacman.py
This particular python script file pacman.py is located in the following folder:
C:\Users\Chris\Dropbox\edX\CS188x\search
So I added this folder to PYTHONPATH and confirmed that is was there using the following code:
>>> import sys
>>> sys.path
['', 'C:\\Program Files (x86)\\Python27\\Lib\\idlelib', 'C:\\Users\\Chris\\Dropbox\\edX\\CS188x\\search', 'C:\\windows\\syste...
I also checked the permissions on this file:
>>> os.access("C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py",os.W_OK)
True
>>> os.access("C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py",os.R_OK)
True
>>> os.access("C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py",os.X_OK)
True
So I am really not sure why I can't run this file, even though its path has been added to PYTHONPATH. Any help would be greatly appreciated. Thank you.
PYTHONPATH is used by the python interpreter. It is not the same as Windows' PATH environment variable. You can't use it as a search path for passing files to the interpreter on the command line.
So, you need to specify a valid path to the file. Either by using he same command as you've been trying with the difference being your current directory is the same as the location of pacman.py, or by specifying the full path to the file.
did you try running the script from its directory?
i can only guess, but maybe its some issue with the file being located inside your dropbox folder...
python "C:\Users\Chris\Dropbox\edX\CS188x\search\pacman.py"
or
cd C:\Users\Chris\Dropbox\edX\CS188x\search\
"c:\prorgam files (x86)\python27\python" pacman.py
SOLVED! So the comments were right in that I had to change the directory to the location of the file, but what was missing was that I had to edit the system environment variable PATH to include the location of python.exe, which is my case was C:\program files (x86)\python27 but for most people is just C:\python27. Thanks everyone for all your help!
I'm on Mac OS 10.6 Snow Leopard and I'm trying to add a directory to my PATH variable so I can run a tiny script I wrote by just typing: python alarm.py at the terminal prompt.
I put the path in my .profile file and it seems to show up when I echo $PATH, but python still can't find script the that I've put in that directory.
Here's the contents of my .profile file in my home directory:
~ toby$ vim .profile
export PATH=/Users/tobylieven/Documents/my_scripts:$PATH
Here's the output of echo $PATH, where all seems well:
~ toby$ echo $PATH
/Users/tobylieven/Documents/my_scripts:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
Here's the script I'm trying to run:
~ toby$ ls /Users/tobylieven/Documents/my_scripts
-rwxrwxrwx# 1 tobylieven staff 276 17 Jan 21:17 alarm.py
Here's the command I'm trying to use to run the script and the fail message I'm getting instead:
~ toby$ python alarm.py
python: can't open file 'alarm.py': [Errno 2] No such file or directory
If anyone has an idea what I might be doing wrong, that'd be great.
Thanks a lot.
PATH is only for executables, not for python scripts. Add the following to the beginning of your Python script:
#!/usr/bin/env python
and run
sudo chmod a+x /Users/tobylieven/Documents/my_scripts/alarm.py
Then, you can type just alarm.py to execute your program.
Which python are you targeting?
Did you install it with brew? It uses a different path.
which python3 or which python
Choose the one you want
Copy that output
Paste it at the top of your python file
add a #! in front of that path so it looks something like
#!/usr/local/bin/python3
Make sure to change the file permissions
chmod +x filename
Put that file in a folder that is in your path
Not sure if your folder is in your path?
echo $path
How to add that folder to your path?
Find your path first
echo $HOME
If you are using bash or zsh you might have something like this
In ~/.bash_profile or ~/.bashrc or ~/.zshrc at the bottom of your file
export PYTHON_UTILS="$HOME/code/python/utils"
export PATH="$PYTHON_UTILS:$PATH"
Consider removing the .py from your file bc it is not needed in this case
Close and open your terminal, which is sourcing your file by its path
And now you should be able to treat your python file similar to a bash command
You don't need to use python3 filename.py to run the file, you can just use filename
From anywhere on your filesystem!
change alarm.py to include:
#!/bin/python
as the very first line in the file.
(or /usr/bin/python, depending on where you python interpreter is located. You can figure this out by typing: which python in the terminal.)
You can then just run alarm.py instead of python alarm.py.
e.g.:
~ toby$ alarm.py
And phihag who beat me by a few seconds is right, you need to add execute permissions (via chmod) to alarm.py.
You need to modify the Python specific path variable: PYTHONPATH.
So:
export PYTHONPATH=/Users/tobylieven/Documents/my_scripts
should get you working.
See: Python Module Search path
Something of interest that I really struggled with on OS X coming from Window, is that you its very hard to get the directory of your current script.
I found this.
#! /bin/zsh
cd "${0:h}"
Now you could execute a python file relative to the executed script instead of having to know the exact path where your python file is.
This might or might not help but I use this a lot to make my scripts and .command files work better.