Function open() doesn't work in Visual Studio Code - python

I have a few projects for which I have to edit .txt documents and I am using Visual Studio Code. When I just use the standard IDLE everything works fine, but when I try it in VS-Code I get the following error code:
Traceback (most recent call last):
File "c:\Users\User\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\ptvsd_launcher.py", line 43, in <module>
main(ptvsdArgs)
File "c:\Users\User\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
run()
File "c:\Users\User\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 316, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 262, in run_path
return (_run_module_code(code, init_globals, run_name, pkg_name=pkg_name, script_name=fname))
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 96, in _run_module_code
_run_code(code, mod_globals, init_globals, mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "c:\Users\User\Desktop\VSCode\test.py", line 1, in <module>
hlayers = open("hiddenvalues.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'hiddenvalues.txt'
PS C:\Users\User\Desktop>
The Code used in the Test is:
f = open("test.txt", "r")
print(f.read())

Without being given a path name, open would try to find the given file name in the interpreter's current working directory, which in Visual Studio Code may be different from your IDLE environment.
Instead you should always pass to open the full path name to the file unless you are certain of the current working directory being the correct one (perhaps by calling os.chdir first).

Related

vscode attached to Docker container - module not found

I'm running vscode debugger inside Docker container but it started showing an error:
That is very strange because when I open python shell in the same vscode window and import the same module import works just fine. So I need to find the reason why debugger doesn't see the modules
The full error code:
root#854c8a51d1f6:/opt/HonkioServer# python3 entrypoints/api2/docker_entry
Traceback (most recent call last):
File "entrypoints/api2/docker_entry", line 3, in <module>
from index import app
File "/opt/HonkioServer/entrypoints/api2/index.py", line 9, in <module>
from honkio.db.Application import ApplicationModel
ModuleNotFoundError: No module named 'honkio'
root#854c8a51d1f6:/opt/HonkioServer# cd /opt/HonkioServer ; /usr/bin/env /bin/python3 /root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 39239 -- /opt/HonkioServer/entrypoints/api2/docker_entry
Traceback (most recent call last):
File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
cli.main()
File "/root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
run()
File "/root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
runpy.run_path(target, run_name="__main__")
File "/root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
File "/root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
File "/opt/HonkioServer/entrypoints/api2/docker_entry", line 3, in <module>
from index import app
File "/opt/HonkioServer/entrypoints/api2/index.py", line 9, in <module>
from honkio.db.Application import ApplicationModel
ModuleNotFoundError: No module named 'honkio'
My debugger config file launch.json
{
"name": "Python: File",
"type": "python",
"request": "launch",
"program": "/opt/HonkioServer/entrypoints/api2/docker_entry",
"justMyCode": true
}
Finally I found the source of this issue - it was in the file that directly imported to docker-entry file and modules from project directories were imported in a strange way:
# This line adds to modules imports that added below cd two levels up
sys.path.insert(0, os.path.dirname(__file__) + "/../..")
import honkio
However, right after I change this file in vs code, formatter changed lines order automatically placing that line at the bottom of all the other imports. Therefore file imports break and it shows module not found error

Pyinstaller not building because permission denied to USS.jtx

I am getting the following error when using the pyinstaller (pyinstaller --onefile main.py --name pbot.exe) on windows:
329097 INFO: checking PKG
329097 INFO: Building PKG because PKG-00.toc is non existent
329099 INFO: Building PKG (CArchive) PKG-00.pkg
Cannot find ('nltk_data\AppData\Local\Comms\UnistoreDB\USS.jtx', 'C:\Users\adwaith\AppData\Local\Comms\UnistoreDB\USS.jtx', 1, 'x')
Traceback (most recent call last):
File "c:\users\adwaith\anaconda3\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\users\adwaith\anaconda3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\adwaith\anaconda3\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
File "c:\users\adwaith\anaconda3\lib\site-packages\PyInstaller\__main__.py", line 114, in run
run_build(pyi_config, spec_file, **vars(args))
File "c:\users\adwaith\anaconda3\lib\site-packages\PyInstaller\__main__.py", line 65, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "c:\users\adwaith\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 734, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "c:\users\adwaith\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 681, in build
exec(code, spec_namespace)
File "C:\Users\adwaith\Documents\pbot_win\pbot.spec", line 20, in <module>
exe = EXE(pyz,
File "c:\users\adwaith\anaconda3\lib\site-packages\PyInstaller\building\api.py", line 433, in __init__
self.pkg = PKG(self.toc, cdict=kwargs.get('cdict', None),
File "c:\users\adwaith\anaconda3\lib\site-packages\PyInstaller\building\api.py", line 199, in __init__
self.__postinit__()
File "c:\users\adwaith\anaconda3\lib\site-packages\PyInstaller\building\datastruct.py", line 160, in __postinit__
self.assemble()
File "c:\users\adwaith\anaconda3\lib\site-packages\PyInstaller\building\api.py", line 283, in assemble
archive = CArchiveWriter(self.name, srctoc + mytoc,
File "c:\users\adwaith\anaconda3\lib\site-packages\PyInstaller\archive\writers.py", line 336, in __init__
super(CArchiveWriter, self).__init__(archive_path, logical_toc)
File "c:\users\adwaith\anaconda3\lib\site-packages\PyInstaller\archive\writers.py", line 63, in __init__
self._add_from_table_of_contents(logical_toc)
File "c:\users\adwaith\anaconda3\lib\site-packages\PyInstaller\archive\writers.py", line 88, in _add_from_table_of_contents
self.add(toc_entry) # The guts of the archive.
File "c:\users\adwaith\anaconda3\lib\site-packages\PyInstaller\archive\writers.py", line 383, in add
fh = open(pathnm, 'rb')
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\adwaith\\AppData\\Local\\Comms\\UnistoreDB\\USS.jtx'
I have checked that the location and the USS.jtx files exist on my machine. Also, I tried searching about USS.jtx file but it keeps showing up in the context of git. I do have a Github desktop, so I thought maybe using pyinstaller from inside the git repo is causing problems. I then copied the entire repo to another location on my computer, but the problem persists.
I'm not normally a Windows guy, but I saw your post on Linkedin about this.
It seems to me that the file exists, but you don't have permission to read it. Have a look at this article and ensure that you have permission to read the file.
A quick and dirty way to check this is to just try opening the file in notepad or whatever the basic text editor is called in Windows.
If notepad gives an error on trying to open the file, then you need to fix the permissions. If notepad can open it without any changes to the permissions, then you have some oddness in how you are running pyinstaller.

FileNotFoundError with blend2bam

So I tried converting a .blend file to .bam with blend2bam as suggested by the official Panda3D page but I am getting a FileNotFoundError.
I am using Windows with Python3.7 (from the Panda3D install).
I tried a lot of possibilities of how one could address the src and dst but it still did not work.
C:\Panda3D-1.10.6-x64\mystuff\pandatest>blend2bam C:\Panda3D-1.10.6-x64\mystuff\pandatest\car_model.blend C:\Panda3D-1.10.6-x64\mystuff\pandatest\
Traceback (most recent call last):
File "c:\panda3d-1.10.6-x64\python\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\panda3d-1.10.6-x64\python\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Panda3D-1.10.6-x64\python\Scripts\blend2bam.exe\__main__.py", line 9, in <module>
File "c:\panda3d-1.10.6-x64\python\lib\site-packages\blend2bam\cli.py", line 195, in main
use_gltf28 = blenderutils.is_blender_28(args.blender_dir)
File "c:\panda3d-1.10.6-x64\python\lib\site-packages\blend2bam\blenderutils.py", line 22, in is_blender_28
output = subprocess.check_output([binpath, '--version'])
File "c:\panda3d-1.10.6-x64\python\lib\subprocess.py", line 411, in check_output
**kwargs).stdout
File "c:\panda3d-1.10.6-x64\python\lib\subprocess.py", line 488, in run
with Popen(*popenargs, **kwargs) as process:
File "c:\panda3d-1.10.6-x64\python\lib\subprocess.py", line 800, in __init__
restore_signals, start_new_session)
File "c:\panda3d-1.10.6-x64\python\lib\subprocess.py", line 1207, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden
Ran into same error. It calls blender so needs to know where to find it:
blend2bam --blender-dir "C:\Program Files\Blender Foundation\Blender 2.82" test.blend test_bam_dir
My workaround is using Blender2.7 and Yabee.
I've had the same problem today and I think I've solve it eventually. You need to use blender installation directory as argument. Here is mine:
File names:
(source) >> cube.blend
(dest)>> cube.bam
Example:
blend2bam cube.blend cube.bam --blender-dir D:\Programs\Blender
The trick is path must be simple. No spaces etc.
I hope it helps
Niyazi

python pants ModuleNotFoundError when using python 3

I am new to python and attempting to setup up a mono repo using pants as the build system. All has been going well, until I told pants to use a python 3 interpreter by setting in pants.ini
[python-setup]
interpreter_constraints: ["CPython>=3.6.5"]
I have a setup a local library python_library which is imported into a python_binary.
In other words, for an identical code base,
running ./pants run ... on python 2.7 works perfectly
running ./pants run ... with the above interpreter_constraints fails with ModuleNotFoundError
The stack trace I am getting looks like
Traceback (most recent call last):
File "/home/daniel/PycharmProjects/analytics-platform/.pants.d/run/py/CPython-3.6.8/8c5d2b01b2fb9952ae4d6116e537a04edd7039e8/.bootstrap/pex/pex.py", line 397, in execute
exit_code = self._wrap_coverage(self._wrap_profiling, self._execute)
File "/home/daniel/PycharmProjects/analytics-platform/.pants.d/run/py/CPython-3.6.8/8c5d2b01b2fb9952ae4d6116e537a04edd7039e8/.bootstrap/pex/pex.py", line 329, in _wrap_coverage
return runner(*args)
File "/home/daniel/PycharmProjects/analytics-platform/.pants.d/run/py/CPython-3.6.8/8c5d2b01b2fb9952ae4d6116e537a04edd7039e8/.bootstrap/pex/pex.py", line 360, in _wrap_profiling
return runner(*args)
File "/home/daniel/PycharmProjects/analytics-platform/.pants.d/run/py/CPython-3.6.8/8c5d2b01b2fb9952ae4d6116e537a04edd7039e8/.bootstrap/pex/pex.py", line 442, in _execute
return self.execute_entry(self._pex_info.entry_point)
File "/home/daniel/PycharmProjects/analytics-platform/.pants.d/run/py/CPython-3.6.8/8c5d2b01b2fb9952ae4d6116e537a04edd7039e8/.bootstrap/pex/pex.py", line 540, in execute_entry
return runner(entry_point)
File "/home/daniel/PycharmProjects/analytics-platform/.pants.d/run/py/CPython-3.6.8/8c5d2b01b2fb9952ae4d6116e537a04edd7039e8/.bootstrap/pex/pex.py", line 547, in execute_module
runpy.run_module(module_name, run_name='__main__')
File "/usr/lib/python3.6/runpy.py", line 208, in run_module
return _run_code(code, {}, init_globals, run_name, mod_spec)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/daniel/PycharmProjects/analytics-platform/.pants.d/pyprep/sources/b9e34cbd28ac4d65dc0c5e39ad35ba34da17a128/src/main.py", line 1, in <module>
from str_utils import normalize
ModuleNotFoundError: No module named 'str_utils'
Is there anything I need to add to the pants.ini file when using CPython>=3.6.5?
Please note - the code used works on python2 and python3 when executed manually

Cannot uninstall pycrypto: cannot find pycrypto-2.6-py2.7-win32.egg

I was installing pycrypto, and was careless and installed the win32 version instead. Unfortunately, when I go to uninstall it, it cannot find the file specified (full error below). Thanks for any help.
Uninstalling pycrypto-2.6:
c:\python27\lib\site-packages\pycrypto-2.6-py2.7-win32.egg
Proceed (y/n)? y
Successfully uninstalled pycrypto-2.6
Traceback (most recent call last):
File "c:\python27\lib\runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "c:\python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Python27\Scripts\pip.exe\__main__.py", line 9, in <module>
File "c:\python27\lib\site-packages\pip\__init__.py", line 233, in main
return command.main(cmd_args)
File "c:\python27\lib\site-packages\pip\basecommand.py", line 252, in main
pip_version_check(session)
File "c:\python27\lib\site-packages\pip\utils\outdated.py", line 102, in
pip_version_check
installed_version = get_installed_version("pip")
File "c:\python27\lib\site-packages\pip\utils\__init__.py", line 838, in
get_installed_version
working_set = pkg_resources.WorkingSet()
File "c:\python27\lib\site-packages\pip\_vendor\pkg_resources\__init__.py",
line 644, in __init__
self.add_entry(entry)
File "c:\python27\lib\site-packages\pip\_vendor\pkg_resources\__init__.py",
line 700, in add_entry
for dist in find_distributions(entry, True):
File "c:\python27\lib\site-packages\pip\_vendor\pkg_resources\__init__.py",
line 1949, in find_eggs_in_zip
if metadata.has_metadata('PKG-INFO'):
File "c:\python27\lib\site-packages\pip\_vendor\pkg_resources\__init__.py",
line 1463, in has_metadata
return self.egg_info and self._has(self._fn(self.egg_info, name))
File "c:\python27\lib\site-packages\pip\_vendor\pkg_resources\__init__.py",
line 1823, in _has
return zip_path in self.zipinfo or zip_path in self._index()
File "c:\python27\lib\site-packages\pip\_vendor\pkg_resources\__init__.py",
line 1703, in zipinfo
return self._zip_manifests.load(self.loader.archive)
File "c:\python27\lib\site-packages\pip\_vendor\pkg_resources\__init__.py",
line 1643, in load
mtime = os.stat(path).st_mtime
WindowsError: [Error 2] The system cannot find the file specified:
'c:\\python27\\lib\\site-packages\\pycrypto-2.6-py2.7-win32.egg'
The key parts of the output are:
Uninstalling pycrypto-2.6:
c:\python27\lib\site-packages\pycrypto-2.6-py2.7-win32.egg
Proceed (y/n)? y
Usually, there's a long list of files here. In your case, it's only an .egg. Which means that you installed the package as a packed .egg (thus most probably, you installed it with easy_install rather than pip: pip always unpacks .eggs).
Now,
WindowsError: [Error 2] The system cannot find the file specified:
'c:\\python27\\lib\\site-packages\\pycrypto-2.6-py2.7-win32.egg'
Means that the .egg was already deleted (probably you deleted it by hand, or a previous uninstallation process was interrupted), but installation metadata wasn't. IIRC it's kept in a file or directory named something like pycrypto-2.6-py2.7-win32.egg-info.
So, locate it in site-packages and delete it.

Categories

Resources