Creating jupyter notebook with Python code - python

I am creating a jupyter notebook programatically with Python. Using the nbformat library.
Once I create the file:
fname = f'{filename}.ipynb'
with open(fname, 'w') as notebook:
nbf.write(nb, notebook)
I open the file with Jupyter Lab and get the following error in the terminal console:
Fatal Python error: init_import_size: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
File "/Users/sota/anaconda3/lib/python3.8/site.py", line 580, in <module>
main()
File "/Users/sota/anaconda3/lib/python3.8/site.py", line 567, in main
known_paths = addsitepackages(known_paths)
File "/Users/sota/anaconda3/lib/python3.8/site.py", line 350, in addsitepackages
addsitedir(sitedir, known_paths)
File "/Users/sota/anaconda3/lib/python3.8/site.py", line 208, in addsitedir
addpackage(sitedir, name, known_paths)
File "/Users/sota/anaconda3/lib/python3.8/site.py", line 169, in addpackage
exec(line)
File "<string>", line 1, in <module>
File "/Users/sota/anaconda3/lib/python3.8/importlib/util.py", line 14, in <module>
from contextlib import contextmanager
File "/Users/sota/anaconda3/lib/python3.8/contextlib.py", line 5, in <module>
from collections import deque
File "/Users/sota/anaconda3/lib/python3.8/collections/__init__.py", line 24, in <module>
import heapq as _heapq
File "/Users/sota/anaconda3/lib/python3.8/heapq.py", line 581, in <module>
from _heapq import *
KeyboardInterrupt
I have been researching a lot, but I don't get to find the error... some guidance on why this is happening?
PD: the only solution I've found so far is openinig the notebook with vscode and somehow, the notebook gets fixed.

It was actually a very newbie error. I had written the names of the folders with accented characters. Therefore, Jupyter Lab couldn't recognize correctly the files.

Related

Can't start Python, import error

Yesterday I installed the mysql connector package, today I can't start python anymore. Anacona won't start nor directly Python 3.6. The Anaconda prompt delivered:
Microsoft Windows [Version 10.0.17134.48]
(c) 2018 Microsoft Corporation. Alle Rechte vorbehalten.
C:\Users\ric22>python.exe -m idlelib.idle
Error processing line 1 of C:\Users\ric22\AppData\Local\Programs\Python\Python36\lib\site-packages\protobuf-3.5.2.post1-py3.6-nspkg.pth:
Failed to import the site module
Traceback (most recent call last):
File "C:\Users\ric22\AppData\Local\Programs\Python\Python36\lib\site.py", line 168, in addpackage
exec(line)
File "<string>", line 1, in <module>
File "D:\Programme\Anaconda\pkgs\sqlalchemy-1.1.13-py36h5948d12_0\Lib\site-packages\sqlalchemy\dialects\mysql\types.py", line 9, in <module>
from ... import exc, util
ImportError: attempted relative import with no known parent package
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\ric22\AppData\Local\Programs\Python\Python36\lib\site.py", line 544, in <module>
main()
File "C:\Users\ric22\AppData\Local\Programs\Python\Python36\lib\site.py", line 531, in main
known_paths = addsitepackages(known_paths)
File "C:\Users\ric22\AppData\Local\Programs\Python\Python36\lib\site.py", line 328, in addsitepackages
addsitedir(sitedir, known_paths)
File "C:\Users\ric22\AppData\Local\Programs\Python\Python36\lib\site.py", line 207, in addsitedir
addpackage(sitedir, name, known_paths)
File "C:\Users\ric22\AppData\Local\Programs\Python\Python36\lib\site.py", line 178, in addpackage
import traceback
File "C:\Users\ric22\AppData\Local\Programs\Python\Python36\lib\traceback.py", line 5, in <module>
import linecache
File "C:\Users\ric22\AppData\Local\Programs\Python\Python36\lib\linecache.py", line 8, in <module>
import functools
File "C:\Users\ric22\AppData\Local\Programs\Python\Python36\lib\functools.py", line 22, in <module>
from types import MappingProxyType
File "D:\Programme\Anaconda\pkgs\sqlalchemy-1.1.13-py36h5948d12_0\Lib\site-packages\sqlalchemy\dialects\mysql\types.py", line 9, in <module>
from ... import exc, util
ImportError: attempted relative import with no known parent package
Does anyone have an idea how to make Python run again? Thanks in Advance
Ric
Thanks amphibient,
I had set this environment variable 'cause I couldn't make mysql package work on Spyder:
D:\Programme\Anaconda\pkgs\sqlalchemy-1.1.13-py36h5948d12_0\Lib\site-packages\sqlalchemy\dialects\mysql\types.py
(Now I see .py is obviously no folder ;))
This messed everything up. I removed it and installed the package with
conda install mysql-connector-python typed into conda path.
Now everything works.

Include python extension module written in C - Error importing MAXREPEAT

I have written a Python extension module in C. In order to compile the C code to create a proper .py, I tried to use gcc and Anaconda, but for some reason there are a number of functions in my code that are not available in the python27.lib provided in Anaconda. If I use another python27.lib file, I get far more error.
So, instead I tried writing a python script that creates the extension. The following code was included in the python script to generate the c extension:
from distutils.core import setup, Extension
# the c extension module
extension_mod = Extension("chr", ["chr.c"])
setup(name = "chr", ext_modules=[extension_mod])
Now, when I try to run this script in cmd, using the following command, I get the error mentioned below. It seems like the problem is some module named MAXREPEAT, but I am sure that if I figure out how to add that one, another will pop-up. Am I doing something fundamentally wrong?
I tried doing something similar with a simple helloworld.c extension, and had similar issues.
python testchr.py build_ext --include
C:\Users\ininaa\Documents\Ladi\python>python testchr.py build_ext --include
Traceback (most recent call last):
File "C:\Python27_win32\Lib\site.py", line 548, in <module>
main()
File "C:\Python27_win32\Lib\site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "C:\Python27_win32\Lib\site.py", line 266, in addusersitepackages
user_site = getusersitepackages()
File "C:\Python27_win32\Lib\site.py", line 241, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "C:\Python27_win32\Lib\site.py", line 231, in getuserbase
USER_BASE = get_config_var('userbase')
File "C:\Python27_win32\Lib\sysconfig.py", line 516, in get_config_var
return get_config_vars().get(name)
File "C:\Python27_win32\Lib\sysconfig.py", line 449, in get_config_vars
import re
File "C:\Python27_win32\Lib\re.py", line 105, in <module>
import sre_compile
File "C:\Python27_win32\Lib\sre_compile.py", line 14, in <module>
import sre_parse
File "C:\Python27_win32\Lib\sre_parse.py", line 17, in <module>
from sre_constants import *
File "C:\Python27_win32\Lib\sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT

BeautifulSoup4 in Sagemath

I'm trying to perform web scraping with BS4 and then do some algebra work with the results in Sage 6.2 (in Lion 10.7.5). I assumed that /Applications/Sage-6.2.app/Contents/Resources/sage/sage -i beautifulsoup4 would work, but the result is
Attempting to download package beautifulsoup4
>>> Checking online list of optional packages.
[Traceback (most recent call last):
File "<stdin>", line 35, in <module>
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python/urllib.py", line 240, in retrieve
fp = self.open(url, data)
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python/urllib.py", line 208, in open
return getattr(self, name)(url)
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python/urllib.py", line 359, in open_http
return self.http_error(url, fp, errcode, errmsg, headers)
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python/urllib.py", line 376, in http_error
return self.http_error_default(url, fp, errcode, errmsg, headers)
File "<stdin>", line 17, in http_error_default
IOError: [Errno 404] Not Found: '//www.sagemath.org/spkg/optional/list'
Error: failed to download http://www.sagemath.org/spkg/optional/list, aborting
Next, I tried /Applications/Sage-6.2.app/Contents/Resources/sage/sage -sh <<< "easy_install --verbose pip" to get bs4 via pip. That succeeded; however /Applications/Sage-6.2.app/Contents/Resources/sage/local/bin/pip2.7 install beautifulsoup4 failed with error message
Traceback (most recent call last):
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/site.py", line 548, in <module>
main()
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/site.py", line 266, in addusersitepackages
user_site = getusersitepackages()
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/site.py", line 241, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/site.py", line 231, in getuserbase
USER_BASE = get_config_var('userbase')
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/sysconfig.py", line 516, in get_config_var
return get_config_vars().get(name)
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/sysconfig.py", line 449, in get_config_vars
import re
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/re.py", line 105, in <module>
import sre_compile
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/sre_compile.py", line 14, in <module>
import sre_parse
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/sre_parse.py", line 17, in <module>
from sre_constants import *
File "/Applications/Sage-6.2.app/Contents/Resources/sage/local/lib/python2.7/sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
Next, I tried upgrading to Sage 6.6. That went smoothly, as did the installation of bs4. However, when I opened the notebook, I was unable to run code cells, because the "Shift-Enter" command only registered as "Enter" and gave me a new line instead of evaluating the code. Additionally, there was no "evaluate" button. Any recommendations to solve any of these issues would be greatly appreciated.
Unfortunately, Sage has changed how it handles optional packages like BS so that older versions may not be able to use them. You can always go to a mirror, though, like http://mirrors.mit.edu/sage/spkg/optional/ download it, and put it in your (old Sage's) optional spkg directory and try doing sage -i path/to/file.
As to the notebook issue, that should be a matter of doing a "hard cache reset" in the browser due to different jQuery versions, see e.g. here.

when i import arcpy in python idle i get an error

when i type import arcpy i get this error
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
from arcpy import da
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\__init__.py", line 21, in <module>
from arcpy.geoprocessing import gp
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\geoprocessing\__init__.py", line 14, in <module>
from _base import *
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 578, in <module>
env = GPEnvironments(gp)
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 575, in GPEnvironments
return GPEnvironment(geoprocessor)
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 531, in __init__
self._refresh()
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 533, in _refresh
envset = (set(env for env in self._gp.listEnvironments()))
RuntimeError: NotInitialized
what should i do ?
i need to use arcpy in eclipse also and i can't!
This sometimes happens when there are not enough licenses to go around, or if your organization has disabled automated library downloading as part of their software suite.
Check what version of Python you're using in Eclipse, I know friends who've had to download condas in order to import arcpy without fail.

Python Cmd/Powershell error "Traceback (Most recent call last)"

I was giving a look at the "Learn Python The Hard Way" tutorial and when it told me to try it out by typing Python in Powershell after I installed it just kept giving me this:
PS C:\Users\Zach> python
Traceback (most recent call last):
File "C:\Python27\lib\site.py", line 548, in <module>
main()
File "C:\Python27\lib\site.py", line 530, in main
known_paths = addusersitepackages(known_paths)
File "C:\Python27\lib\site.py", line 266, in addusersitepackages
user_site = getusersitepackages()
File "C:\Python27\lib\site.py", line 241, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "C:\Python27\lib\site.py", line 231, in getuserbase
USER_BASE = get_config_var('userbase')
File "C:\Python27\lib\sysconfig.py", line 516, in get_config_var
return get_config_vars().get(name)
File "C:\Python27\lib\sysconfig.py", line 449, in get_config_vars
import re
File "C:\Python27\lib\re.py", line 105, in <module>
import sre_compile
File "C:\Python27\lib\sre_compile.py", line 14, in <module>
import sre_parse
File "C:\Python27\lib\sre_parse.py", line 17, in <module>
from sre_constants import *
File "C:\Python27\lib\sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
I also tried running through the command prompt and got the same error. I've installed and uninstalled python about 3 times and have done a few reboots to no avail. I am very new to python and I'm confused on what to do here. I would also appreciate any links to other python tutorials.
In this case, from my understanding, you have no modules called "MAXREPEAT" or you(or the installer) have installed it somewhere wrong. A reinstallation or redirecting the path should get it to work.

Categories

Resources