Moving files with UNC destination path in Celery task - python

I'm currently trying to transfer a file as a Celery Task on Django from local disk to another machine via UNC path and I've run into quite a few road blocks.
I've used a package called win_unc which works fine in an independent script. However when I use the same code as a celery task, I run into the following error:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\celery\task\trace.py", line 228, in trace_task
R = retval = fun(*args, **kwargs)
File "C:\Python27\lib\site-packages\celery\task\trace.py", line 415, in __protected_call__
return self.run(*args, **kwargs)
File "Path\to\file\file.py", line 96, in movefile
conn.mount()
File "C:\Python27\lib\site-packages\win_unc\connecting.py", line 163, in mount
self.connect()
File "C:\Python27\lib\site-packages\win_unc\connecting.py", line 63, in connect
self._connect_with_creds(self.get_username(), self.get_password())
File "C:\Python27\lib\site-packages\win_unc\connecting.py", line 118, in _connect_with_creds
run(command)
File "C:\Python27\lib\site-packages\win_unc\internal\shell.py", line 29, in run
raise ShellCommandError(command, process.returncode)
ShellCommandError: The command `NET USE "Y:" "\\UNC\path" /PERSISTENT:NO` exited with error code 2.
The other modules I used to move the file were os and shutil.
I used shutil to copy the file over to the UNC destination and got the following error:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\celery\task\trace.py", line 228, in trace_task
R = retval = fun(*args, **kwargs)
File "C:\Python27\lib\site-packages\celery\task\trace.py", line 415, in __protected_call__
return self.run(*args, **kwargs)
File "Path\to\file\file.py", line 96, in movefile
shutil.copy(source.strip(), dest.strip())
File "C:\Python27\lib\shutil.py", line 117, in copy
copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 83, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 22] invalid mode ('wb') or filename: '\\\\UNC\\path\\somefile.csv'
This was error with os.rename
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\celery\task\trace.py", line 228, in trace_task
R = retval = fun(*args, **kwargs)
File "C:\Python27\lib\site-packages\celery\task\trace.py", line 415, in __protected_call__
return self.run(*args, **kwargs)
File "Path\to\file\file.py", line 95, in movefile
os.rename(source, dest)
WindowsError: [Error 1326] Logon failure: unknown user name or bad password
All of the above work fine when I use an independent python script to move the file. This also works from the python command line. I don't understand why it fails as a Celery task or if there is any other reason.
I use Python 2.7.3 and Django 1.4.2.
I'd really appreciate any help with this.

Related

ipython3 - KeyError: 'ipython_dir' - FileNotFoundError: [Errno 2] No such file or directory: '/Users/fp/.ipython'

My computer shut down while running a simple code on Spyder. Ever since it has restarted, it seems that /Users/fp/.ipython has not been found anymore.
To be more specific, I run:
ipython3
And I get the following error message:
Traceback (most recent call last):
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/traitlets/traitlets.py", line 537, in get
value = obj._trait_values[self.name]
KeyError: 'ipython_dir'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/fp/opt/anaconda3/bin/ipython3", line 11, in <module>
sys.exit(start_ipython())
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/IPython/__init__.py", line 130, in start_ipython
return launch_new_instance(argv=argv, **kwargs)
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/traitlets/config/application.py", line 845, in launch_instance
app.initialize(argv)
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/traitlets/config/application.py", line 88, in inner
return method(app, *args, **kwargs)
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/IPython/terminal/ipapp.py", line 308, in initialize
super(TerminalIPythonApp, self).initialize(argv)
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/traitlets/config/application.py", line 88, in inner
return method(app, *args, **kwargs)
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/IPython/core/application.py", line 458, in initialize
self.init_profile_dir()
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/IPython/core/application.py", line 362, in init_profile_dir
p = ProfileDir.find_profile_dir_by_name(self.ipython_dir, self.profile, self.config)
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/traitlets/traitlets.py", line 577, in __get__
return self.get(obj, cls)
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/traitlets/traitlets.py", line 540, in get
default = obj.trait_defaults(self.name)
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/traitlets/traitlets.py", line 1580, in trait_defaults
return self._get_trait_default_generator(names[0])(self)
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/traitlets/traitlets.py", line 977, in __call__
return self.func(*args, **kwargs)
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/IPython/core/application.py", line 174, in _ipython_dir_default
d = get_ipython_dir()
File "/Users/fp/opt/anaconda3/lib/python3.9/site-packages/IPython/paths.py", line 70, in get_ipython_dir
os.makedirs(ipdir, exist_ok=True)
File "/Users/fp/opt/anaconda3/lib/python3.9/os.py", line 225, in makedirs
mkdir(name, mode)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/fp/.ipython'
I checked and indeed I do not have a /Users/fp/.ipython folder. On the contrary, I still have a /Users/fp/.jupyter folder for example.
ipython3 --version
7.31.1
conda --version
conda 22.9.0
python3 --version
Python 3.9.13
Anyone could help me with that?
Thank you

jupyter nbconvert to PDF raises PermissionError: [WinError 5]

I'm trying to convert a Jupyter notebook into a PDF file but I keep getting the following error:
[NbConvertApp] Converting notebook ejercicios.ipynb to pdf
Traceback (most recent call last):
...
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\nbconvert\exporters\exporter.py", line 143, in from_notebook_node
nb_copy, resources = self._preprocess(nb_copy, resources)
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\nbconvert\exporters\exporter.py", line 318, in _preprocess
nbc, resc = preprocessor(nbc, resc)
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\nbconvert\preprocessors\base.py", line 47, in __call__
return self.preprocess(nb, resources)
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\nbconvert\preprocessors\base.py", line 69, in preprocess
nb.cells[index], resources = self.preprocess_cell(cell, resources, index)
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\nbconvert\preprocessors\convertfigures.py", line 45, in preprocess_cell
output.data[self.to_format] = self.convert_figure(
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\nbconvert\preprocessors\svg2pdf.py", line 125, in convert_figure
shell = self.command.format(from_filename=input_filename,
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\traitlets\traitlets.py", line 575, in __get__
return self.get(obj, cls)
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\traitlets\traitlets.py", line 538, in get
default = obj.trait_defaults(self.name)
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\traitlets\traitlets.py", line 1578, in trait_defaults
return self._get_trait_default_generator(names[0])(self)
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\traitlets\traitlets.py", line 975, in __call__
return self.func(*args, **kwargs)
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\nbconvert\preprocessors\svg2pdf.py", line 77, in _command_default
major_verison = self.inkscape_version.split('.')[0]
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\traitlets\traitlets.py", line 575, in __get__
return self.get(obj, cls)
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\traitlets\traitlets.py", line 538, in get
default = obj.trait_defaults(self.name)
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\traitlets\traitlets.py", line 1578, in trait_defaults
return self._get_trait_default_generator(names[0])(self)
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\traitlets\traitlets.py", line 975, in __call__
return self.func(*args, **kwargs)
File "c:\users\netoo\appdata\local\programs\python\python38\lib\site-packages\nbconvert\preprocessors\svg2pdf.py", line 57, in _inkscape_version_default
p = subprocess.Popen([self.inkscape, '--version'],
File "c:\users\netoo\appdata\local\programs\python\python38\lib\subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "c:\users\netoo\appdata\local\programs\python\python38\lib\subprocess.py", line 1311, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
PermissionError: [WinError 5] Acceso denegado
Looks like the default permissions for that command aren't enough to create a process of Inkscape, but I'm already using the command line with administrator privileges.
I was able to convert a notebook to PDF successfully past year, it's the same machine but I was using another version of Python, 3.6 or 3.7, does that have to do to with this?
I reported this issue here https://github.com/jupyter/nbconvert/issues/1525
My temporary solution was to use the absolute path as a string literal where I installed Inkscape instead of calling self.inkscape, in my case the path is 'C:\\Program Files\\Inkscape\\inkscape.exe':
https://github.com/jupyter/nbconvert/blob/88baf7a9ca724d74c9f890a60d5c3680e8959a14/nbconvert/preprocessors/svg2pdf.py#L57
in that line I have p = subprocess.Popen(['C:\\Program Files\\Inkscape\\inkscape.exe', '--version'].
I printed self.inkscape and the output was 'C:\Program Files\Inkscape\inkscape.exe'. I thought it was because of the double back slash so I tried os.path.abspath(self.inkscape) but it was adding the current path from where I'm calling the command at the beginning. I also tried pathlib.Path(self.inkscape) but it didn't work, so I have no idea what the problem could be.

Python coverage crashing when using unittest.mock

I have a Python library in which I am setting up some tests. As parts of the tests I am using MagicMock to mock some features like JSON load and files openning and closing. The tests work, everything works as expected.
The issue comes when I try to run the tests with coverage. Since I introduced the new tests that use mock I get coverage crashing.
This is the part where I am implementing the mocking:
def setUp(self):
# Setup the content of the config files for the tests
json.load = MagicMock(side_effect=file_content)
# Opening a file returns the name of the file
def get_mock_context(filename):
mock_context = MagicMock()
mock_context.__enter__.return_value = filename
mock_context.__exit__.return_value = False
return mock_context
builtins.open = MagicMock(side_effect=get_mock_context)
As mentioned, when I run the tests everything seems to be working fine
python -m unittest discover -s tests -p "*Tests.py"
...........................
----------------------------------------------------------------------
Ran 27 tests in 1.322s
OK
Nonetheless, as soon as I introduce coverage:
coverage run -m unittest discover -s tests -p "*Tests.py"
...........................
----------------------------------------------------------------------
Ran 27 tests in 1.759s
OK
Traceback (most recent call last):
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 627, in do_run
self.run_python_module(args[0], args)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/execfile.py", line 122, in run_python_module
run_python_file(pathname, args, package=packagename, modulename=modulename, path0=path0)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/execfile.py", line 192, in run_python_file
exec(code, main_mod.__dict__)
File "/anaconda3/envs/c360/lib/python3.7/unittest/__main__.py", line 18, in <module>
main(module=None)
File "/anaconda3/envs/c360/lib/python3.7/unittest/main.py", line 101, in __init__
self.runTests()
File "/anaconda3/envs/c360/lib/python3.7/unittest/main.py", line 273, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: False
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/envs/c360/bin/coverage", line 10, in <module>
sys.exit(main())
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 756, in main
status = CoverageScript().command_line(argv)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 491, in command_line
return self.do_run(options, args)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 641, in do_run
self.coverage.save()
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 782, in save
self.data_files.write(self.data, suffix=self.data_suffix)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/data.py", line 680, in write
data.write_file(filename)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/data.py", line 467, in write_file
with open(filename, 'w') as fdata:
File "/anaconda3/envs/c360/lib/python3.7/unittest/mock.py", line 951, in __call__
return _mock_self._mock_call(*args, **kwargs)
File "/anaconda3/envs/c360/lib/python3.7/unittest/mock.py", line 1017, in _mock_call
result = effect(*args, **kwargs)
TypeError: get_mock_context() takes 1 positional argument but 2 were given
I have no idea what the issue might be. I have tried excluding the lines in the tests files that include mocking with pragma: no cover but that did nothing. I cannot find a way to make this work again. Any ideas or something you think I might have missed?
EDIT:
I just cleaned a bit the code y pasted before. If I run coverage specifying that I want to to use as source only the files in my local directory (to avoid using coverage in python libraries etc) I get the following error. Still not working, but a different one:
Traceback (most recent call last):
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 627, in do_run
self.run_python_module(args[0], args)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/execfile.py", line 122, in run_python_module
run_python_file(pathname, args, package=packagename, modulename=modulename, path0=path0)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/execfile.py", line 192, in run_python_file
exec(code, main_mod.__dict__)
File "/anaconda3/envs/c360/lib/python3.7/unittest/__main__.py", line 18, in <module>
main(module=None)
File "/anaconda3/envs/c360/lib/python3.7/unittest/main.py", line 101, in __init__
self.runTests()
File "/anaconda3/envs/c360/lib/python3.7/unittest/main.py", line 273, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: False
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/anaconda3/envs/c360/bin/coverage", line 10, in <module>
sys.exit(main())
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 756, in main
status = CoverageScript().command_line(argv)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 491, in command_line
return self.do_run(options, args)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/cmdline.py", line 641, in do_run
self.coverage.save()
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 781, in save
self.get_data()
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 834, in get_data
self._post_save_work()
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 864, in _post_save_work
self._find_unexecuted_files(src)
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 913, in _find_unexecuted_files
for file_path, plugin_name in itertools.chain(py_files, plugin_files):
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/control.py", line 910, in <genexpr>
py_files = ((py_file, None) for py_file in find_python_files(src_dir))
File "/anaconda3/envs/c360/lib/python3.7/site-packages/coverage/files.py", line 416, in find_python_files
del dirnames[:]
TypeError: 'tuple' object does not support item deletion

Unable to launch Spyder due to FileNotFoundError

The error is the following:
Traceback (most recent call last):
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\app\mainwindow.py", line 3208, in main
mainwindow = run_spyder(app, options, args)
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\app\mainwindow.py", line 3084, in run_spyder
main.setup()
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\app\mainwindow.py", line 828, in setup
self.workingdirectory = WorkingDirectory(self, self.init_workdir, main=self)
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\plugins\workingdirectory.py", line 159, in __init__
self.chdir(workdir)
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\plugins\workingdirectory.py", line 296, in chdir
self.refresh_plugin()
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\plugins\workingdirectory.py", line 204, in refresh_plugin
self.save_wdhistory()
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\plugins\workingdirectory.py", line 234, in save_wdhistory
encoding.writelines(text, self.LOG_PATH)
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\utils\encoding.py", line 236, in writelines
return write(os.linesep.join(lines), filename, encoding, mode)
File "C:\Users\Shubham\Anaconda3\lib\site-packages\spyder\utils\encoding.py", line 227, in write
with open(filename, mode) as textfile:
FileNotFoundError: [Errno 2] No such file or directory: '%USERPROFILE\\.spyder-py3\\workingdir'
(Spyder maintainer here) This is a bug in Spyder and we'll fix it in our 3.3.0 version, to be released later in June/2018.

Robot Frame work: Error While running .robot file in python interpreter using robot.run (windows environment)

I am trying to execute Run_Keyword.robot made with RIDE file which is in directory 'Robot' from python interpreter and getting following error. I am able to run this .py file from cmd without any error and it gives expected results. What can be the reason for this. I am a newbie to Robot Framework. Please help with this.
from robot import run
run('C:\\Users\\uvijayac\\Desktop\\Robot')
The Error I am getting is as follows.
Traceback (most recent call last):
File "C:\Users\uvijayac\Desktop\Robot\rf.py", line 27, in <module>
run_tests()
File "C:\Users\uvijayac\Desktop\Robot\rf.py", line 23, in run_tests
report=report_file)
File "C:\Python27\lib\site-packages\robot\run.py", line 471, in run
return RobotFramework().execute(*datasources, **options)
File "C:\Python27\lib\site-packages\robot\utils\application.py", line 83, in execute
return self._execute(list(arguments), options)
File "C:\Python27\lib\site-packages\robot\utils\application.py", line 89, in _execute
return self._report_error(unicode(err), help=True)
File "C:\Python27\lib\site-packages\robot\utils\application.py", line 110, in _report_error
self._logger.error(message)
File "C:\Python27\lib\site-packages\robot\output\loggerhelper.py", line 59, in error
self.write(msg, 'ERROR')
File "C:\Python27\lib\site-packages\robot\output\loggerhelper.py", line 62, in write
self.message(Message(message, level, html))
File "C:\Python27\lib\site-packages\robot\output\logger.py", line 109, in message
logger.message(msg)
File "C:\Python27\lib\site-packages\robot\output\monitor.py", line 66, in message
self._writer.error(msg.message, msg.level, clear=self._running_test)
File "C:\Python27\lib\site-packages\robot\output\monitor.py", line 142, in error
self._highlight('[ ', level, ' ] ' + message, error=True)
File "C:\Python27\lib\site-packages\robot\output\monitor.py", line 158, in _highlight
self._write(before, newline=False, error=error)
File "C:\Python27\lib\site-packages\robot\output\monitor.py", line 154, in _write
stream.flush()
IOError: [Errno 9] Bad file descriptor
>>>

Categories

Resources