I'm using this code to create a file with "AAA" written in it. It's working in the regular python IDLE but isn't working in VScode.
with open("another.txt","w") as myfile:
myfile.write("AAA")
If no error is reported, then the file is successfully created somewhere. As you are using a relative path the file is probably saved to current working directory.
You can find it either via the path displayed in the terminal you are using, or in your Python code:
from pathlib import Path
print(Path.cwd())
Related
Python : 3.5
IDE : Visual code
Platform : win 10 64 bit
First i created a virtual env _kerasVenv and then activated the env and then installed pandas using pip.
This is my directory structure:
I added a python script in Exercise files folder where I am trying to read .csv file using pandas
test= pd.read_csv('test.csv', encoding='utf-8')
.csv file and python script are in the same folder so wrong path is not the issue.But i am getting below error:
Unable to open 'parsers.pyx': Unable to read file (Error: File not found (c:\users\anubhav.jhalani\downloads\ex_files_building_deep_learning_apps\pandas\_libs\parsers.pyx)).
Can someone explain why python is looking for pandas in c:\users\anubhav.jhalani\downloads\ex_files_building_deep_learning_apps folder and why parsers.pyx file does not exist in original pandas folder which are in _kerasVenv folder?
How can i get rid of this error?
Update: I found out while hovering on import pandas as pd statement that it is looking for pandas module in c:\users\anubhav.jhalani\downloads\ex_files_building_deep_learning_apps . Why it is happening?
I think it is easier to always give the total file path.
Instead of:
test= pd.read_csv('test.csv', encoding='utf-8')
try to use:
test = pd.read_csv('C:/users/anubhav.jhalani/downloads/ex_files_building_deep_learning_apps/test.csv', endcoding='utf-8')
this should help, you can also have a look here, what syntax you need to write it:
Windows path in Python
You can also get the full path in your windows explorer, if you are unsure where it is saved.
In my case it also wasn't able to find the file even in the same directory and with the complete path. I figured it out that this issue was happening only in my VSCode editor and when I opened the same notebook in jupyter lab it was perfectly working. So then I tried one thing that make it work for me in VSCode as well was that I put my file in the directory other than "C". For example I put my file in the "D" directory and in VSCode it becomes able to work fine.
I am practicing some API work in python 3.7 using API star 0.5.X and my python script can't find the .json file that is in the same folder as the python file. I am working on and running the script with Atom editor and I am working in a venv, which is fairly new to me.
I am using a helper function to load in the JSON data using a "with open()" statement. I have tried using the relative and absolute file paths, and in both instances it is unable to locate the file. I have tried launching the file in Atom using terminal and the MacOS finder.
This is what I have so far:
import json
from typing import List
import os
from apistar import App, Route, types, validators
from apistar.http import JSONResponse
print(os.getcwd())
os.chdir('/Users/{myusernamehere}/100days/apistar')
print(os.getcwd())
#helpers
def _load_employee_data():
with open('employees.json') as f:
employees = json.loads(f.read())
return employees
the second print statement prints the correct file path, being the one that 'employees.json' and 'app.py' are located in.
Since the problem is specific to your setup, it's hard to reproduce or provide a solution in code. Your code itself looks to be fine, but there are two things that are likely to be the cause of your issues:
When your script is running, Python needs access to the appropriate source folders and installed packages; you should let something like virtualenv manage this through a virtual environment. From the terminal, you can load the appropriate virtual environment with:/path/to/your/venv/Scripts/activate.sh
If you do, you should expect your script to find the same libraries it did during development in that virtual environment. Make sure you include something like a requirements.txt in your project to allow easy reinstalling of the same modules on a different machine, in a new virtual environment.
Your script, when run by Python, has a 'working directory'. This is the directory that Python is started from and your script not being able to find the file (even though it may be in the same folder as the script itself) is probably due to Python being started from a different directory.
This was a problem due to how the Atom editor works. It was solved by switching to vim.
I only partially understand but apparently this had something to do with Atom having a separate temp directory for working files, or something of that nature. When using vim to edit the script, and then calling it in the terminal the problem was resolved.
Okay, So i had the same issue with i.c.w. VScode :
file = open('file.txt')
print(file.name)
resulted in
FileNotFoundError
file.txt was 100% in the same folder... According to finder on my Mac, ánd the folder column in VS code!
i was pulling out my hair. Switched a lot of interpreters, local python and Conda, to Python 3.8 instead of 3.9, back to python 2.8.
Nothing mattered.
Till I changed :
file = open('file.txt') to: file = open('file.txt', 'a')
It didn't suddenly work, but I saw immediately in the "folder column" of VScode a new file.txt file popping up. In an entirely different folder then where the pythonfile.py was located. So after that; I pushed all local repo's to their remotes; deleted the whole caboodle, and installed them one by one in a new folder through git clone. I opened a new workspace added those project folders and since then it works like a charm.
What exactly went wrong ; im sorry, I have no idea. But for me, a fresh install of the repo's and VScode workspace is what did the trick.
I recently had the same error, on Visual Studio Code, I managed to solve it by instead of clicking the Run Python button, I used the terminal to cd into the project directory and run the python script like that, and no problems!
I simply wanted to read a JSON file using this code:
import json
with open("file.json") as File:
print(json.load(File.read()))
When I try to run it using the VSCode debugger, I get the error:
[Errno 2] No such file or directory: 'file.json'
But when I run it from the Terminal using python file.py it works.
The problem is, that VSCode somehow uses an other "Working Directory" because when I run os.getcwd() in VSCode, I get the path to the parent folder of the folder the python script is in.
When I run it from the Terminal, I get the right path.
Why is that?
Just to point out: the problem is NOT the print statement/json. The same error shows up when I only try to open the file without anything else and then pass.
import os
dir_path = os.getcwd()
Use this to get your current directory. You'll be able to discern where Python is running from. You can also use the full filepath for your JSON file.
import json
with open("fullpath/to/json/file/file.json") as File:
print(json.loads(File.read()))
As discussed in comments, your problem turned out to be the environment you were working in. When the program was executed from terminal, it worked and found the file. That is most likely because of the way your virtual environment is set up in VS Code. A virtual environment or venv as it is called, is an isolated environment of Python interpreter separate from your global Python install. It is useful when you are working on two different projects that require different versions of libraries. For example; a project that uses Django 1.10 and one that uses 1.9, so you don't have to keep shuffling between installing and uninstalling them.
A virtual environment is a directory tree which contains Python
executable files and other files which indicate that it is a virtual
environment.
As explained here, your .json file was most likely outside of your project virtual environment, and that is why it could not find it. I hope that helps you understand it.
You can verify if a file exists using os package:
import os.path
print(os.path.isfile("file.json"))
this should print True if the file exists.
Also you can try using the absolute path to make sure.
import json
with open('/Users/my_pc/Downloads/example_2.json') as f:
data = json.load(f)
print(data)
you can load your json by this way and give exact path of your directory
where your json file is present.
Set-up
I run a script on my computer, located in the directory Users/path/to/my/script.py.
In the script, I use the path to the script, e.g.,
sub_path = 'Users/path/to/my/'
os.chdir(sub_path + 'other_script/')
As you can see, I define sub_path in the code 'manually'.
Problem
I don't want to define the sub_path manually, I'd rather have Python do it for me.
I'm looking for something similar to the code I use to obtain the current working directory: os.getcwd(), but then a code to obtain the directory of the current file.
I mainly find answers similar to this one, which says,
os.path.abspath(os.path.dirname(__file__))
but in the Spyder & Anaconda set-up, this generates a NameError: name '__file__' is not defined.
What can I do?
You if you want to move back one folder/directory you use the .. in your file path.
os.chdir('../other_scripts/')
will work. You may fine it helpful to view this or the wiki.
If you want to move from where you currently are you can use './new_dir/'. If you want to automate how to find other files you may want to read here which says to use os.walk. This may be the same question.
Mark8888 pointed out to run the whole script (run file (F5)) instead of just pieces of the script
this way multiple approaches should work to get the script file location and change the current working directory
import os
# directory of script file
print(os.path.abspath(os.path.dirname(__file__)))
# change current working directory
os.chdir(os.path.abspath(os.path.dirname(__file__)))
# current working directory
print(os.getcwd())
also
import os
import sys
# directory of script file
print(os.path.abspath(os.path.dirname(sys.argv[0])))
# change current working directory
os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
# current working directory
print(os.getcwd())
I add the following lines to any script I run in case I need to access data relative to the location of the script
import sys
script = sys.argv[0]
print(script)
'C:/SomeFolder/A_Subfolder/CurrentlyRunningScript.py' # changed obviously
First, save your Jupyter Notebook. Second, locate the directory your Jupyter Notebook is stored in. Thirdly, ensure that your Jupyter Notebook and CSV file are in the same place.
I am trying to schedule my python code (.py) to run in Windows 10 using task scheduler. In order to do so, I need to indicate where my python.exe file is located.
I downloaded the latest Anaconda to my Windows 10 machine and run my code in jupyter notebook successfully so I should have python.exe somewhere.
I cannot find where the "python.exe" file path is stored in my computer. Thoughts?
Please see sample example below:
You can get it quite easily using the os module:
First open a Python interpreter, not through terminal but directly (there should be a shortcut on your start menu).
Next get the os module:
import os
And use the getcwd() method to find it:
os.getcwd()
The better way to do it. If Python is added to path.
Open a command prompt window. Not the Python interpreter or open Power Shell and type:
where python
Another way to do it is to type python.exe in the file explorer search bar at C:.
Get yourself a copy of Search Everything and type python.exe into its search box. That will find all Pythons available on your PC.