VS Code - printed file paths when selecting Run Python File in Terminal - python

When in VC Code, I right-click and select "Run Python File in Terminal," I get two long folder paths (see below) that I am sure have a reason to exist.
That said, when I right-click and select "Run Python File in Terminal," what what is the reason why I see there two long file paths? And is there a reason why I would want to leave them there? If not, is there a way to just see the current folder "Python_Deep_Dive_Part_1>" and then the output of the python file (e.g. "Hello World").

The first file path is the absolute path to your selected Python interpreter and the second is the absolute path to the location of the file you're executing. Both paths are absolute as there is no guarantee that your terminal is open to the same directory it started in and VS Code doesn't provide a way for the extension to know what the current directory is.

I have solved for first part and still searching for summarization to file.py
VSCcode > settings.json:
"python.pythonPath": "py"

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("","/")

Python PyCharm can't find executables in /usr/local/bin [duplicate]

Recently, I'm unable to use relative paths in my code while using PyCharm. For instance, a simple open('test.txt', 'r') will not work - whereupon I am sure the file exists in the same level as the running py file. PyCharm will return this error.
FileNotFoundError: [Errno 2] No such file or directory:
After reading answers online on StackOverflow, I have tried multiple options including:
Changing test.txt to ./test.txt
Closing project, deleting the .idea folder, open the folder with code.
Reinstalling as well as installing the latest version of PyCharm.
Invalidating caches and restarting.
None of these options have worked for me. Is there someway I can tell PyCharm to refresh the current working directory (or even to see where it thinks the current working directory is)?
Edit: I should note that running the script in a terminal window will work. This appears to be a problem with PyCharm and not the script.
Change:
Run > Edit Configurations > Working directory,
which sets the working directory for a specific project. (This is on a Mac)
I have Pycharm 4.5, so things might have changed a bit.
Try going to Settings > Project > Project Structure
On this dialog, click your folder that has the source code in it, and then click the blue folder in the menu to note it as "source" folder. I believe this fixes a lot of the path issues in Pycharm
Here is the link to "content roots": https://www.jetbrains.com/pycharm/help/content-root.html
Current version 2019.2 somehow ignores "source root" from the "project structure". Here's how to actually enforce it:
Run -> Edit Configurations -> Python -> "Edit Templates" -> fill out "Working Directory"
__file__ refers to file path. So you can use the following to refer file in the same directory:
import os
dirpath = os.path.dirname(__file__)
filepath = os.path.join(dirpath, 'test.txt')
open(filepath, 'r')
In PyCharm, click on "run/edit configurations..."
Then find your script file in the "Python" dropdown menu. Check the "Working Directory" entry and change it if necessary.
EXACT ANSWER TO SOLVE THIS ISSUE ,,
GO TO EDIT CONFIGURATION (just LEFT side of GREEN CODE RUNNER ICON)
click on python (not any specific python script) ONLY SELECT PYTHON
then below right side click on [edit configuration templetes]
select current working dir by going into those blocks
It will change the CWD of all python file that exists in project folder..
then all file will understand the RELATIVE PATH that starts from your actual project name..
i hope this will resolve all your issue related path.
Sometimes it is different. I solved my problem by clicking "Run" at the Pycharm's toolbar and then "Edit Configurations..." and I change my Interpreter to another actual one. Just changing it in the settings does not help, but this opperation already does ;)
I too had the same issue few minutes ago...but,with the latest version of PyCharm it is resolved by simply using the relative path of that file..
For instance, a simple f = open('test', 'r') will work.
A little clarification for mac users. In mac, what #andere said above is correct for setting working directory. However, if your code is in a different folder, say working_dir/src/ (like classic java/scala file structure) in that case you still need to set your Sources Root. In mac's PyCharm this can be done by right clicking on the src/ folder > Mark Directory as > Sources Root. Helped me with lot of similar import issues. Hope this helps someone.

How to change the VisualStudio's run shortcut run command (python)

So i recently had a problem that regarded the command that is executed in vsc's integrated terminal every time the little triangular button on top right corner is pressed
It executes a command that consists of file path to python.exe file, and the currently selected file:
[path to current folder]>C:/Users/[username]/AppData/Local/Programs/Python/Python38-32/python.exe [currently selected file]
and i had problems with that, it didn't recognize some modules. So i found out if i do it by running the command python [currently selected file] it works flawlessly.
I found a setting in the python extension that is labeled python.pythonPath, which i edited from original to "python.pythonPath": "python", which i thought would work but it didn't. I have been digging trough the extention settings for some time now, and i have not been able to find this setting.
Is it even possible? Do i do it right? Is it even done by the python extention?
If you use cmd, it can run the Python files properly,it is recommended that you check whether the environment variables are correct. Reference:This.
To better run Python files in vscode, you could set up the vcode virtual environment.
As for the path 'python.pythonPath', you could refer to:pythonpath.

How to set working directory for projects in PyCharm?

Recently, I'm unable to use relative paths in my code while using PyCharm. For instance, a simple open('test.txt', 'r') will not work - whereupon I am sure the file exists in the same level as the running py file. PyCharm will return this error.
FileNotFoundError: [Errno 2] No such file or directory:
After reading answers online on StackOverflow, I have tried multiple options including:
Changing test.txt to ./test.txt
Closing project, deleting the .idea folder, open the folder with code.
Reinstalling as well as installing the latest version of PyCharm.
Invalidating caches and restarting.
None of these options have worked for me. Is there someway I can tell PyCharm to refresh the current working directory (or even to see where it thinks the current working directory is)?
Edit: I should note that running the script in a terminal window will work. This appears to be a problem with PyCharm and not the script.
Change:
Run > Edit Configurations > Working directory,
which sets the working directory for a specific project. (This is on a Mac)
I have Pycharm 4.5, so things might have changed a bit.
Try going to Settings > Project > Project Structure
On this dialog, click your folder that has the source code in it, and then click the blue folder in the menu to note it as "source" folder. I believe this fixes a lot of the path issues in Pycharm
Here is the link to "content roots": https://www.jetbrains.com/pycharm/help/content-root.html
Current version 2019.2 somehow ignores "source root" from the "project structure". Here's how to actually enforce it:
Run -> Edit Configurations -> Python -> "Edit Templates" -> fill out "Working Directory"
__file__ refers to file path. So you can use the following to refer file in the same directory:
import os
dirpath = os.path.dirname(__file__)
filepath = os.path.join(dirpath, 'test.txt')
open(filepath, 'r')
In PyCharm, click on "run/edit configurations..."
Then find your script file in the "Python" dropdown menu. Check the "Working Directory" entry and change it if necessary.
EXACT ANSWER TO SOLVE THIS ISSUE ,,
GO TO EDIT CONFIGURATION (just LEFT side of GREEN CODE RUNNER ICON)
click on python (not any specific python script) ONLY SELECT PYTHON
then below right side click on [edit configuration templetes]
select current working dir by going into those blocks
It will change the CWD of all python file that exists in project folder..
then all file will understand the RELATIVE PATH that starts from your actual project name..
i hope this will resolve all your issue related path.
Sometimes it is different. I solved my problem by clicking "Run" at the Pycharm's toolbar and then "Edit Configurations..." and I change my Interpreter to another actual one. Just changing it in the settings does not help, but this opperation already does ;)
I too had the same issue few minutes ago...but,with the latest version of PyCharm it is resolved by simply using the relative path of that file..
For instance, a simple f = open('test', 'r') will work.
A little clarification for mac users. In mac, what #andere said above is correct for setting working directory. However, if your code is in a different folder, say working_dir/src/ (like classic java/scala file structure) in that case you still need to set your Sources Root. In mac's PyCharm this can be done by right clicking on the src/ folder > Mark Directory as > Sources Root. Helped me with lot of similar import issues. Hope this helps someone.

How to open a file inside a folder which is in the same folder as a Python program?

Basically I am looking for a simple way to open text file in a folder that is inside the same folder as the program.
My directory structure looks like this:
/programfolder/textfiles/textfile
And I'm trying to use open like this:
text=functionthatgetsfilename()
file=open("textfiles/"+text,"r")
What am I doing wrong? Do I just have a typo somewhere?
You need to know the difference between the Current Directory and the directory your script is in. Your current directory is the directory that you started the application from, in the command line (CMD, SH, etc). You can show that with os.path.normpath(os.curdir).
To solve your problem, you can use
file=open(os.path.join(os.path.dirname(__file__),'holdstextfiles',text),'r')
or
os.chdir(os.path.dirname(__file__))
...
The first solution uses the absolute path to your desired file, which is the same no matter what: it's absolute
The second solution changes the current directory before trying to use the relative path that you're using.

Categories