Skip to content

ImportError: cannot import name 'secure_write' #5014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
GabriellaKwan opened this issue Oct 29, 2019 · 31 comments
Closed

ImportError: cannot import name 'secure_write' #5014

GabriellaKwan opened this issue Oct 29, 2019 · 31 comments

Comments

@GabriellaKwan
Copy link

GabriellaKwan commented Oct 29, 2019

Last time(a day ago) I lauched the notebook and close it, it was running fine. Then I double clicked to open the jupyer-notebook.exe, a jupyter console window showed up and disappeared in 3 seconds and without openning the localhost:8888 page, nor could I manually open the localhost address. I used jupyter notebook command in the cmd window, it shows the following respond:

Traceback (most recent call last):
File "d:\python36\lib\runpy.py", line 193, in run_module_as_main
"main", mod_spec)
File "d:\python36\lib\runpy.py", line 85, in run_code
exec(code, run_globals)
File "D:\Python36\Scripts\jupyter-notebook.EXE_main
.py", line 4, in
File "d:\python36\lib\site-packages\notebook\notebookapp.py", line 81, in
from .services.kernels.kernelmanager import MappingKernelManager
File "d:\python36\lib\site-packages\notebook\services\kernels\kernelmanager.py", line 19, in
from jupyter_client.session import Session
File "d:\python36\lib\site-packages\jupyter_client_init
.py", line 4, in
from .connect import *
File "d:\python36\lib\site-packages\jupyter_client\connect.py", line 35, in
from jupyter_core.paths import jupyter_data_dir, jupyter_runtime_dir, secure_write
ImportError: cannot import name 'secure_write'

I tried reinstalling jupyter notebook using pip uninstall jupyter and pip install jupyter,but there is no use. The command jupyter notebook --help gives the same response as above.

I tried to search for similar issues online but failed finding any. If anyone knows the solution, please help. Thank you in advance!

@SangamSwadiK
Copy link

@GabriellaKwan Try this
pip install --upgrade jupyter_client

@sowole-aims
Copy link

Awesome. work perfectly.

@Caiphe
Copy link

Caiphe commented Jan 4, 2020

Thanks saved my frustration

@oscaralarcon
Copy link

Hi @SangamSwadiK , help me please

Traceback (most recent call last):
File "c:\users\nb-zenta\anaconda3\lib\runpy.py", line 193, in run_module_as_main
"main", mod_spec)
File "c:\users\nb-zenta\anaconda3\lib\runpy.py", line 85, in run_code
exec(code, run_globals)
File "C:\Users\NB-ZENTA\Anaconda3\Scripts\jupyter-notebook.EXE_main
.py", line 5, in
File "c:\users\nb-zenta\anaconda3\lib\site-packages\notebook\notebookapp.py", line 78, in
from .services.kernels.kernelmanager import MappingKernelManager
File "c:\users\nb-zenta\anaconda3\lib\site-packages\notebook\services\kernels\kernelmanager.py", line 19, in
from jupyter_client.session import Session
File "c:\users\nb-zenta\anaconda3\lib\site-packages\jupyter_client_init
.py", line 4, in
from .connect import *
File "c:\users\nb-zenta\anaconda3\lib\site-packages\jupyter_client\connect.py", line 35, in
from jupyter_core.paths import jupyter_data_dir, jupyter_runtime_dir, secure_write
ImportError: cannot import name 'secure_write' from 'jupyter_core.paths' (c:\users\nb-zenta\anaconda3\lib\site-packages\jupyter_core\paths.py)

@PhoenixEdward
Copy link

@oscaralarcon I also just received this error. The solution above did not work for me. However, running Git Bash as administrator did for me.

Perhaps try this?

@SangamSwadiK
Copy link

SangamSwadiK commented Jan 24, 2020

@oscaralarcon Try the last one in this ,(he replaces the paths.py file )
https://community.deepcognition.ai/t/importerror-cannot-import-name-secure-write/1134

@oscaralarcon
Copy link

@PhoenixEdward , @SangamSwadiK
it was solved, thanks very much, bless

@khuara17
Copy link

khuara17 commented Feb 3, 2020

@GabriellaKwan Try this
pip install --upgrade jupyter_client

Thanks for help , it worked

@kevin-bates
Copy link
Member

Right on - glad folks are succeeding with the helpful tips! I'm going to close this due to the success.

Generally speaking, ensure that jupyter_client >= 5.3.4 and jupyter_core >= 4.6.1 for issues surrounding secure_write().

@betur-toufik
Copy link

thanks it works with me

@AurelieGiraud1
Copy link

Awesome! thanks that work fine for me too!

@idjey-zz
Copy link

idjey-zz commented Jun 6, 2020

Thanks for suggestion, it worked

@gangadhar20
Copy link

it is working

@DelusJean-Nelka
Copy link

thanks

@shalaby64
Copy link

Many thanks !!!

@HaneulKim214
Copy link

jupyter-client version = 6.1.11, jupyter-core ==4.7.0 and still doesn't work...

@kevin-bates
Copy link
Member

Hi @HaneulKim214 - Perhaps there's a slightly different issue here or you're checking an environment in which the notebook server is not running within. Could you please post your notebook server's console output that contains the traceback of your issue?

You might also want to try pip install --upgrade for both jupyter_client and jupyter_core to confirm.

@dhamelse
Copy link

dhamelse commented Apr 2, 2021

I am still getting ImportError: cannot import name 'secure_write' using jupyter_client version 5.3.4 and jupyter_core 4.7.1. I have already tried replacing paths.py in the env/site-packages/jupyter_core/paths.py.

any ideas?

@kevin-bates
Copy link
Member

This all implies you're dealing with one env but executing another. Are you sure you're looking at the active env when determining version information?

Do you have a traceback exhibiting the ImportError?

@dhamelse
Copy link

dhamelse commented Apr 2, 2021

Yes. See below

Traceback (most recent call last):
File "/home/dh47l/.conda/envs/cmapPy_py27/bin/jupyter-notebook", line 7, in
from notebook.notebookapp import main
File "/home/dh47l/.conda/envs/cmapPy_py27/lib/python2.7/site-packages/notebook/notebookapp.py", line 81, in
from .services.kernels.kernelmanager import MappingKernelManager
File "/home/dh47l/.conda/envs/cmapPy_py27/lib/python2.7/site-packages/notebook/services/kernels/kernelmanager.py", line 19, in
from jupyter_client.session import Session
File "/home/dh47l/.conda/envs/cmapPy_py27/lib/python2.7/site-packages/jupyter_client/init.py", line 4, in
from .connect import *
File "/home/dh47l/.conda/envs/cmapPy_py27/lib/python2.7/site-packages/jupyter_client/connect.py", line 35, in
from jupyter_core.paths import jupyter_data_dir, jupyter_runtime_dir, secure_write
ImportError: cannot import name secure_write

Also, this is the current env conda list output (with package versions)

# packages in environment at /home/dh47l/.conda/envs/cmapPy_py27:

_libgcc_mutex 0.1 main
asn1crypto 1.4.0 py_0
attrs 20.3.0 pyhd3eb1b0_0
backports 1.0 pyhd3eb1b0_2
backports.shutil_get_terminal_size 1.0.0 pyhd3eb1b0_3
backports_abc 0.5 py_1
blas 1.0 mkl
bleach 3.3.0 pyhd3eb1b0_0
ca-certificates 2021.1.19 h06a4308_1
certifi 2020.6.20 pyhd3eb1b0_3
cffi 1.14.0 py27he30daa8_1
chardet 3.0.4 py27_1003
cmappy 3.2.0 py27_0 bioconda
configparser 4.0.2 py27_0
contextlib2 0.6.0.post1 py_0
cryptography 2.8 py27h1ba5d50_0
dbus 1.13.18 hb2f20db_0
decorator 4.4.2 pyhd3eb1b0_0
defusedxml 0.7.1 pyhd3eb1b0_0
entrypoints 0.3 py27_0
enum34 1.1.6 py27_1
expat 2.3.0 h2531618_2
fontconfig 2.13.1 h6c09931_0
freetype 2.10.4 h5ab3b9f_0
functools32 3.2.3.2 py27_1
futures 3.3.0 py27_0
glib 2.68.0 h36276a3_0
gst-plugins-base 1.14.0 h8213a91_2
gstreamer 1.14.0 h28cd5cc_2
h5py 2.9.0 py27h7918eee_0
hdf5 1.10.4 hb1b8bf9_0
icu 58.2 he6710b0_3
idna 2.10 pyhd3eb1b0_0
importlib_metadata 1.3.0 py27_0
intel-openmp 2020.2 254
ipaddress 1.0.23 py_0
ipykernel 4.10.0 py27_0
ipython 5.8.0 py27_0
ipython_genutils 0.2.0 pyhd3eb1b0_1
ipywidgets 7.6.0 pyhd3eb1b0_1
jinja2 2.11.3 pyhd3eb1b0_0
jpeg 9b h024ee3a_2
jsonschema 3.2.0 py27_0
jupyter 1.0.0 py27_7
jupyter_client 5.3.4 py27_0
jupyter_console 5.2.0 py27_1
jupyter_core 4.6.1 py27_0
libffi 3.3 he6710b0_2
libgcc 7.2.0 h69d50b8_2
libgcc-ng 9.1.0 hdf63c60_0
libgfortran-ng 7.3.0 hdf63c60_0
libpng 1.6.37 hbc83047_0
libsodium 1.0.18 h7b6447c_0
libstdcxx-ng 9.1.0 hdf63c60_0
libuuid 1.0.3 h1bed415_2
libxcb 1.14 h7b6447c_0
libxml2 2.9.10 hb55368b_3
linecache2 1.0.0 py_1
markupsafe 1.1.1 py27h7b6447c_0
mistune 0.8.4 py27h7b6447c_0
mkl 2020.2 256
mkl-service 2.3.0 py27he904b0f_0
mkl_fft 1.0.15 py27ha843d7b_0
mkl_random 1.1.0 py27hd6b4f25_0
more-itertools 5.0.0 py27_0
nbconvert 5.6.1 py27_0
nbformat 4.4.0 py27_0
ncurses 6.2 he6710b0_1
notebook 5.7.10 py27_0
numpy 1.16.6 py27hbc911f0_0
numpy-base 1.16.6 py27hde5b4d6_0
openssl 1.1.1k h27cfd23_0
packaging 20.9 pyhd3eb1b0_0
pandas 0.24.2 py27he6710b0_0
pandoc 2.12 h06a4308_0
pandocfilters 1.4.2 py27_1
pathlib2 2.3.5 py27_0
pcre 8.44 he6710b0_0
pexpect 4.8.0 pyhd3eb1b0_3
pickleshare 0.7.5 py27_0
pip 19.3.1 py27_0
prometheus_client 0.10.0 pyhd3eb1b0_0
prompt_toolkit 1.0.15 py27_0
ptyprocess 0.7.0 pyhd3eb1b0_2
pycparser 2.20 py_2
pygments 2.5.2 py_0
pyopenssl 20.0.1 pyhd3eb1b0_1
pyparsing 2.4.7 pyhd3eb1b0_0
pyqt 5.9.2 py27h05f1152_2
pyrsistent 0.15.6 py27h7b6447c_0
pysocks 1.7.1 py27_0
python 2.7.18 h15b4118_1
python-dateutil 2.8.1 pyhd3eb1b0_0
pytz 2021.1 pyhd3eb1b0_0
pyzmq 18.1.0 py27he6710b0_0
qt 5.9.7 h5867ecd_1
qtconsole 4.7.7 py_0
qtpy 1.9.0 py_0
readline 8.1 h27cfd23_0
requests 2.25.1 pyhd3eb1b0_0
scandir 1.10.0 py27h7b6447c_0
send2trash 1.5.0 pyhd3eb1b0_1
setuptools 44.0.0 py27_0
simplegeneric 0.8.1 py27_2
singledispatch 3.6.1 pyhd3eb1b0_1001
sip 4.19.8 py27hf484d3e_0
six 1.15.0 pyhd3eb1b0_0
sqlite 3.35.3 hdfb4753_0
terminado 0.8.3 py27_0
testpath 0.4.4 pyhd3eb1b0_0
tk 8.6.10 hbc83047_0
tornado 5.1.1 py27h7b6447c_0
traceback2 1.4.0 py_0
traitlets 4.3.3 py27_0
unittest2 1.1.0 pyhd3eb1b0_0
urllib3 1.25.7 py27_0
wcwidth 0.2.5 py_0
webencodings 0.5.1 py27_1
wheel 0.36.2 pyhd3eb1b0_0
widgetsnbextension 3.5.1 py27_0
xz 5.2.5 h7b6447c_0
zeromq 4.3.4 h2531618_0
zipp 0.6.0 py_0
zlib 1.2.11 h7b6447c_3

@dhamelse
Copy link

dhamelse commented Apr 2, 2021

I just noticed the jupyter_core package version is only 4.6.1. updated versions are not available in my miniconda channels, but the current versions are viable according to earlier comments.

@kevin-bates
Copy link
Member

Strange. And do you find secure_write() in /home/dh47l/.conda/envs/cmapPy_py27/lib/python2.7/site-packages/jupyter_core/paths.py?

@dhamelse
Copy link

dhamelse commented Apr 2, 2021

Yes, the function is defined as

@contextmanager
def secure_write(fname, binary=False):
"""Opens a file in the most restricted pattern available for
writing content. This limits the file mode to 0o0600 and yields
the resulting opened filed handle.
Parameters
----------
fname : unicode
The path to the file to write
binary: boolean
Indicates that the file is binary
"""
mode = 'wb' if binary else 'w'
open_flag = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
try:
os.remove(fname)
except (IOError, OSError):
# Skip any issues with the file not existing
pass

if os.name == 'nt':
    if allow_insecure_writes:
        # Mounted file systems can have a number of failure modes inside this block.
        # For windows machines in insecure mode we simply skip this to avoid failures :/
        issue_insecure_write_warning()
    else:
        # Python on windows does not respect the group and public bits for chmod, so we need
        # to take additional steps to secure the contents.
        # Touch file pre-emptively to avoid editing permissions in open files in Windows
        fd = os.open(fname, open_flag, 0o0600)
        os.close(fd)
        open_flag = os.O_WRONLY | os.O_TRUNC
        win32_restrict_file_to_user(fname)

with os.fdopen(os.open(fname, open_flag, 0o0600), mode) as f:
    if os.name != 'nt':
        # Enforce that the file got the requested permissions before writing
        file_mode = get_file_mode(fname)
        if 0o0600 != file_mode:
            if allow_insecure_writes:
                issue_insecure_write_warning()
            else:
                raise RuntimeError("Permissions assignment failed for secure file: '{file}'."
                    " Got '{permissions}' instead of '0o0600'."
                    .format(file=fname, permissions=oct(file_mode)))
    yield f

@kevin-bates
Copy link
Member

This is beginning to exceed my Python experience level for how to go about troubleshooting. I'm assuming that you've tried.

  • forcing the modules' updates? (jupyter_core and jupyter_client)
  • creating a new env?

@dhamelse
Copy link

dhamelse commented Apr 6, 2021

Yes, I have. Thanks for all the help anyways I appreciate it

@kevin-bates
Copy link
Member

Tapping some resources: @bollwyvl - do you have an idea on what might be going on in @dhamelse's scenario (comments within the last 4 days)? It seems the tools show one thing, but the runtime produces another such that secure_write method is not found - even though all indications (version and physical) say it should be present. I have a hunch you've encountered situations like this - probably due to mixing conda and pip or something like that.

@bollwyvl
Copy link
Contributor

bollwyvl commented Apr 6, 2021

Looks like a py27 env... pretty much you'd have to look at what worked last year about this time, and pin to those versions... but even then, some packages had started dropping. I basically wouldn't expect anything less than a year old to work there.

@francoisbres
Copy link

@kevin-bates commented on 28 Jan
You might also want to try pip install --upgrade for both jupyter_client and jupyter_core to confirm.

That worked for me. Thanks

@ndnng
Copy link

ndnng commented Aug 16, 2021

also try
pip install pathlib

@Mary-art-ui
Copy link

@oscaralarcon Try the last one in this ,(he replaces the paths.py file )
https://community.deepcognition.ai/t/importerror-cannot-import-name-secure-write/1134

It really helps for me.

@sondvo
Copy link

sondvo commented Dec 21, 2021

@GabriellaKwan Try this pip install --upgrade jupyter_client

Thank you so much. Work perfectly for jupyter in vscode (with anaconda environment)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests