Reading file from the same directory as the python module - python

Suppose I have got python module foo.py and file myfile.txt that reside in the same directory. foo.py contains the following code to read myfile.txt:
from os import path
myfile_path = path.join(path.dirname(__file__), 'myfile.txt')
myfile = open(myfile_path)
I found myself writing path.join(path.dirname(__file__), '...') over and over again in different modules. Is there a shorter and simpler way to read a file from the same directory as the python module ?

I don't know if this applies to your exact situation, but here is what I have found:
There should be a JSON file that defines how the debugger should work. For me, I use VS Code, and I use the Microsoft Python debugger. It uses a file called launch.json, and contains an array called "configurations":
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole"
]
It is missing a key called "cwd", short for "current working directory" (you can read more about it here). If you add it with the value of blank quotes "", then when Python searches for a file without a specified path, it will search within the same folder. Showing the last few lines of what that looks like:
"program": "${file}",
"console": "internalConsole",
"cwd": ""
]
For more general advice, your debugger, or whatever else is running your Python file, needs to have its working directory correctly defined.
I realize the post is nineteen months old at some point, but I hope this helps someone!

You can use data = open('myfile.txt', 'r').read(), without using path.

Related

I get ImportError when building a python app in VSCode

I'm a python newb, and am trying to build a Github python app in VSCode, link here:
https://github.com/mapsme/osm_conflate
The devs aren't very responsive, and the problem is not very specific to the program, so I'm asking here. I made the launch.json file in the folder /home/janko/source/osm_conflate/.vscode/, the contents here:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${workspaceRoot}/conflate/conflate.py",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}/conflate/",
"args": [
"-p", "/home/janko/Documents/profile.py",
"-o", "josm.osm"
]
}
]
}
And I get an error:
Exception has occurred: ImportError
attempted relative import with no known parent package
File "/home/janko/source/osm_conflate/conflate/conflate.py", line 8, in <module>
from .geocoder import Geocoder
I tried to change the python version from 3 to python2, but the text of the error just changes a bit. I tried to put other folders in the "cwd" property, but it's always the same. I tried to put other .py files in the "program" part of the json, but it doesn't help. What am I doing wrong?
From PEP 328 -- Imports: Multi-Line and Absolute/Relative, we can know
Relative imports use a module’s name attribute to determine that
module’s position in the package hierarchy. If the module’s name does
not contain any package information (e.g. it is set to main ) then
relative imports are resolved as if the module were a top level
module, regardless of where the module is actually located on the file
system.
To solve the encountered error, we may use absolute import instead of relative import.
Change the folder conflate to other name, or else when you use from conflate.geocoder import Geocoder in conflate.py, it will throw a cirle import error.
Copy the content in __init__.py then delete it. Paste the content to a new created __init__.py, then reload the window;
Use pip install lxml to install the required module to the current environmet;
In conflate.py, add sys.path.append('./') and import functions like from conflate.geocoder import Geocoder.
Finally you can run conflate.py successfully:

VSCode debug mode does not set os.getcwd() to the one specified in launch.json

I am trying to debug my python program in VSCode where I'm getting its directory. When I run os.getcwd() from the terminal, I get the correct directory, but when I use the VS Code debug option, it defaults to the "default" path (as set in my registry variable, which is C:\Users<User>\Downloads).
I have created a launch.json file.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "C:\\Users\\<User>\\Documents\\Project\\"
}
]
}
Here I added "cwd", except no matter what value I put here, the value of os.getcwd() returns the default path in debug mode. I have tried putting: the whole path, ${workspaceFolder}, ${fileDirname}, ${fileWorkspaceFolder}.
The launch.json file is in the .vscode folder in my project.
I do not understand why this is happening and would ideally like a fix. None of the other questions on this site on this subject were able to help.
For those that might have the same problem in the future, I found a work-around. (This works without launch.json.)
I manually edited the code from: directory = os.getcwd() to:
dir_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(dir_path)
directory = os.getcwd()
Then the debugger is in the current working directory and is able to see the files I needed it to.
Please use settings similar to the following in "launch.json":
"cwd": "${workspaceFolder}\\a_pythonscript",
When my "main.py" is in the folder "demo_csv" and "lauch.json" uses "cwd": "${workspaceFolder}\\demo",:
Since the python debugging function in VS Code is provided by the Python extension, please try to reinstall the Python extension.

Python in VS Code: Error when importing module from subfolder

I recently started exploring VS Code for developing Python code and I’m running into an issue when I try to import a module from a subfolder. The exact same code runs perfectly when I execute it in a Jupyter notebook (the subfolders contain the __init__.py files etc.) I believe I followed the instructions for setting up the VS Python extension correctly. Everything else except this one import command works well, but I haven’t been able to figure what exactly is going wrong.
The structure of the project is as follows: The root folder, which is set as the cwd contains two subfolders (src and bld). src contains the py-file that imports a module that is saved in foo.pyin the bld-folder using from bld.foo import foo_function
When running the file, I get the following error: ModuleNotFoundError: No module named ‘bld'. I have several Anaconda Python environments installed and get the same problem with each of them. When copying foo.py to the src directory and using from foo import foo_function everything works.
My launch.json file is as follows:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {"PYTHONPATH": "${workspaceFolder}:${workspaceFolder}/bld"},
"console": "integratedTerminal"
}
]
}
Any ideas or help would be greatly appreciated!
Stefan‘s method worked for me.
Taking as example filesystem:
workspaceFolder/folder/subfolder1/subfolder2/bar.py
I wasn't able to import subfolders like:
from folder.subfolder1.subfolder2 import bar
It said: ModuleNotFoundError: No module named 'folder'
I added to .vscode/settings.json the following:
"terminal.integrated.env.osx": {
"PYTHONPATH": "${workspaceFolder}"
}
I also added at the beginning of my code:
import sys
#[... more imports ...]
sys.path.append(workspaceFolder)
# and then, the subfolder import:
from folder.subfolder1.subfolder2 import bar
Now, it works.
Note: all my folders and subfolders have an empty file named __init__.py. I still had to do the steps described above.
VSCode version: 1.52.0 (from 10-dec-2020)
I think I finally figured out the answer myself: The integrated terminal does not scan the PYTHONPATH from the .env-file. When running the file in an integrated window, the PYTHONPATH is correctly taken from .env, however. So in order to run my script in the terminal I had to add the terminal.integrated.env.* line in my settings.json as follows:
{
"python.pythonPath": "/anaconda3/envs/py36/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.flake8Enabled": false,
"python.envFile": "${workspaceFolder}/.env",
"terminal.integrated.env.osx": {
"PYTHONPATH": "${workspaceFolder}"
}
}

Import error when debugging youtube-dl in VS Code

I'm trying to write a new extractor for youtube-dl. First I want to debug the __main__.py to get to know the tool, but I cannot debug using VS Code. Here's my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": ["a_youtube_video"]
}
]
}
My breakpoint is set in the __main__.py, which looks like this:
from __future__ import unicode_literals
# Execute with
# $ python youtube_dl/__main__.py (2.6+)
# $ python -m youtube_dl (2.7+)
import sys
if __package__ is None and not hasattr(sys, 'frozen'):
# direct call of __main__.py
import os.path
path = os.path.realpath(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(os.path.dirname(path)))
import youtube_dl
if __name__ == '__main__':
youtube_dl.main() # Breakpoint here
The error I'm facing is the import youtube_dl line, it reports that there's no module named youtube_dl. What am I missing here?
Edit: I've just found a way to debug it. It said right in the comments of the __main__.py: From 2.7, the program must be run as a module. However I still don't understand this module thing.
Try creating a "Python: Module" debug configuration. There you will see a "module" key in your configuration which you can specify as appropriate to use __main__ (i.e. I don't' know if there's a top-level __main__.py or if its in the package which changes what needs to be specified).

`SyntaxError: invalid syntax` when starting Python script in VS Code on macOS

I'm trying to run a Python script from Visual Studio code, but the script fails to run and crashes with a SyntaxError pointing to the comment at the beginning of launch.json.
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python | Default",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
}
]
}
Terminal Output:
File ".../.vscode/launch.json", line 2
// Use IntelliSense to learn about possible attributes.
^
SyntaxError: invalid syntax
settings.json:
{
"python.pythonPath": "${workspaceFolder}/venv/bin/python"
}
I was working on my Windows machine earlier and all of this worked perfectly fine. For some reason, VSCode is trying to run the launch.json file through Python and // is an invalid comment syntax in Python. If I remove the comments, I get this error:
Traceback (most recent call last):
File ".../.vscode/launch.json", line 8, in <module>
"stopOnEntry": false,
NameError: name 'false' is not defined
If I use Python's False, I don't crash but nothing happens and my script does not run. It seems very much like launch.json is being parsed by Python erroneously. Any fix for this?
I found my problem. I did not update the program key to always point to my main.py. Instead, the current open file was being executed as a Python script -- launch.json Changing the program key or navigating to a different file solved the problem. Obvious once you notice it!
Solution 1
I consider that an easier solution is:
Close the launch.json on the editor group
Open the python file such as main.py to be debugged
[Run]-[Start Debugging] (F5)
As Nick mentioned, when focusing on the launch.json in the editor, the debug system runs on the launch.json itself, not a python file.
Solution 2
Modify the "program" in the launch.json as below:
"program": "${workspaceFolder}/main.py",
It corresponds to
the program key to always point to main.py
as Nick said.
Note that the above modification may not work well if the main.py places in a deep directory.
Closing launch.json if it is open for editing may solve the issue
If launch.json is the latest open file, VSCode may be trying to run launch.json as a Python module (despite the fact that it's clearly not a Python module).
See the NameError in the OP's third screenshot - looks like Python interpreter running against launch.json
(Note: the contribution of this answer is solely to put the crux of Haru's Solution 1.1 and Nick's own self-diagnosis into simple language in the answer's first line)

Categories

Resources