I am learning to import packages in python and facing issue in importing custom packages while debugging code in Thonny IDE.
The issue does not come if I simply run the program.
My relative directory structure is
Compilation\Scripts\tesing_pkg_import.py
Contents of tesing_pkg_import.py is
import pandas as pd
def tes_func():
#Checking for same column name in a single dataframe
testDpCol = [ (11, 'jack', 34, 'Sydney', 5) ]
testDfObj = pd.DataFrame(testDpCol, columns=['ID', 'Name', 'Age', 'Name', 'Experience'])
print(testDfObj.head())
Then in Compilation folder I have tesing_pkg_import_main.py content of which are
import Scripts.tesing_pkg_import as test
test.tes_func()
I have verified
That my parent path is present in syspath
Program is running successfully
Issue comes only when I start debugger in Thonny
init.py file is present in Compilation\Scripts\ folder
The issue logs are printed as follow :
Traceback (most recent call last):
File "D:\***Masked Manually*****\Compilation\tesing_pkg_import_main.py", line 1, in <module>
import Scripts.tesing_pkg_import as test
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 851, in exec_module
File "<frozen importlib._bootstrap_external>", line 988, in get_code
IndexError: list index out of range
Any help is appreciated.
Seems like someone else also faced this issue and raised on github.
Link is to issue is
https://github.com/thonny/thonny/issues/1920
The solution seems to work for me also, fast debug is working which shift-F5 but normal debug is not working which Ctrl-F5.
Anyone who needs help with this can watch the issue on git hub link I have mentioned.
Related
I am trying to set up python in Vim, but I failed to get it to work. It always throws an exception named UnicodeDecodeError.
I have installed gvim on windows 10. And also installed python 3 with the corresponding version.
Vim can find the python37.dll and the command
:echo has('python3')
returns 1 as expected.
My vim with python works only when no modules except for the builtin ones are imported.
For example:
:py3 print('a')
works pretty well.
:py3 import vim
or
:py3 import sys
are also working.
However, if I write a simple python script vim_test.py like this
print('This is for vim test')
then try to import it in vim as
:py3 import vim_test
it will give an exception:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 963, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 906, in _find_spec
File "<frozen importlib._bootstrap_external>", line 1280, in find_spec
File "<frozen importlib._bootstrap_external>", line 1252, in _get_spec
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 9: invalid start byte
It cannot import any python module in Vim.
But it can run this script directly by
:py3file vim_test.py
if the script file vim_test.py is in the current directory.
What could be the reason for this problem?
And how can I solve it?
I expected to be able to use vim plugins written in python.
With this problem, I cannot achieve that.
I've been using tensorflow for quite some time. Recently my scripts run very slow (> 80 sec) as to earlier (< 1 sec). I narrowed the issue down to import tensorflow, which alone is taking all the time (all other libs and ops are running << 1 sec).
I might have a trace, but I don't know what to do with it: When I keyboard-interrupt (Strg+C) the execution during the 80 sec import, usually this is what comes up:
Traceback (most recent call last):
File "/.../py_env/tf_unet/lib/python3.5/site.py", line 703, in <module>
main()
File "/.../py_env/tf_unet/lib/python3.5/site.py", line 694, in main
execsitecustomize()
File "/.../py_env/tf_unet/lib/python3.5/site.py", line 548, in execsitecustomize
import sitecustomize
File "/usr/lib/python3.5/sitecustomize.py", line 3, in <module>
import apport_python_hook
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 954, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 896, in _find_spec
File "<frozen importlib._bootstrap_external>", line 1139, in find_spec
File "<frozen importlib._bootstrap_external>", line 1113, in _get_spec
File "<frozen importlib._bootstrap_external>", line 1225, in find_spec
File "<frozen importlib._bootstrap_external>", line 1264, in _fill_cache
KeyboardInterrupt
Does this mean, something with "filling the caches" (_fill_cache) is wrong? Anyone experience with this? Can I fix this somehow?
What I've tried so far:
I broke down import tensorflow to only modules I need (from tensorflow import train / python_io / compat), with no improvement.
I found other people complaining about long import tensorflow speed here, here and in the corresponding SO question, but in the range of < 10 sec and referring to specific modules (tf.contrib or tf.learn). So not much to learn from there. Also I am using tensorflow 1.4.0 which apparently fixed the problems described there.
Just for reference, I am using this little piece of code to determine the speed:
from timeit import default_timer as timer
print('import tensorflow')
start = timer()
import tensorflow
end = timer()
print('Elapsed time: ' + str(end - start))
This is probably not the only reason that can cause this, but in my experience certainly plays a role. I had serious slowness in importing Tensorflow due to the fact that I had my TF virtual environment on a network drive. Moving the virtual environment to a local hard drive helped quite a bit in this respect.
You could try doing something similar that applies in your environment.
Putting together a new sublime install and trying to install the Control Panel. Have been through all of the standard forums, but just in the position of getting a mystery traceback regardless of installation method.
Traceback (most recent call last):
File "/opt/sublime_text/sublime_plugin.py", line 74, in reload_plugin
m = importlib.import_module(modulename)
File "./importlib/__init__.py", line 90, in import_module
File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
File "/opt/sublime_text/sublime_plugin.py", line 678, in load_module
exec(compile(source, source_path, 'exec'), mod.__dict__)
File "Package Control in /home/slater/.config/sublime-text-3/Installed Packages/Package Control.sublime-package", line 14, in <module>
File "./posixpath.py", line 83, in join
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'
plugins loaded
Unable to open /home/slater/.config/sublime-text-3/Packages/Package Control/Package Control.sublime-settings
Unable to open /home/slater/.config/sublime-text-3/Packages/User/Package Control.sublime-settings
Unable to open /home/slater/.config/sublime-text-3/Packages/Default/Preferences.sublime-settings
Presumably, this traceback is why things aren't functional, but unfortunately it's completely impenetrable. It references code that doesn't exist that seems to be making a relative call to another file that doesn't exist. I tried opening up the Package Control zip file, but since the console isn't giving me a real traceback and there's no file called posixpath.py it's very unhelpful for debugging.
It seems like this is some issue with paths? The call to the sublime files in opt is also throwing me for a loop, but I'm not certain that's related the the problem I'm seeing.
UPDATE: Have tried adding sublime package files to $PATH and $PYTHONPATH. Have also experimented with permissions, all to no avail.
Getting this below error while trying to run ansible(version >2) with python 3.5.2
I have looked into the github issues terming it as resolved, but can't sort out what needs to be done. https://github.com/ansible/ansible/issues/16013
How to resolve this?
virtual#xxxxxxxxxx:~/ansible-spike> ansible all -m ping -vvv
Using /home/virtual/ansible-spike/ansible.cfg as config file
ERROR! Unexpected Exception: name 'urllib2' is not defined
the full traceback was:
Traceback (most recent call last):
File "/home/virtual/.pyenv/versions/3.5.2/bin/ansible", line 92, in <module>
exit_code = cli.run()
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/cli/adhoc.py", line 193, in run
result = self._tqm.run(play)
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/executor/task_queue_manager.py", line 202, in run
self.load_callbacks()
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/executor/task_queue_manager.py", line 171, in load_callbacks
for callback_plugin in callback_loader.all(class_only=True):
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/plugins/__init__.py", line 368, in all
self._module_cache[path] = self._load_module_source(name, path)
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/plugins/__init__.py", line 319, in _load_module_source
module = imp.load_source(name, path, module_file)
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/imp.py", line 172, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 693, in _load
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/plugins/callback/hipchat.py", line 32, in <module>
from ansible.module_utils.urls import open_url
File "/home/virtual/.pyenv/versions/3.5.2/lib/python3.5/site-packages/ansible/module_utils/urls.py", line 330, in <module>
if hasattr(httplib, 'HTTPSConnection') and hasattr(urllib2, 'HTTPSHandler'):
NameError: name 'urllib2' is not defined
Urllib2 is specific to Python v2.
Urllib2 documentation at http://docs.python.org/library/urllib2.html:
The urllib2 module has been split across several modules in Python 3.0
named urllib.request and urllib.error.
I don't think Ansible is compatible with Python 3 yet.
The ansible python API does not support Python 3. The PyPI page lists only 2.6 and 2.7 .
Ansible is currently not able to run with Python3. That is also stated in the linked Github Issue.
If you are using
import urllib2
Instead, you can call the library as
import urllib.request
Then use it like
urllib.request.urlopen('your url goes here')
This should get rid of the error you are getting
After installing Anaconda for Python 3.4 on my Mac I get constant messages saying:
Error in sitecustomize; set PYTHONVERBOSE for traceback:
KeyError: 'PYTHONPATH'
As suggested by a user on another question, I used
PYTHONVERBOSE=1 conda update --all
And received the traceback:
Traceback (most recent call last):
File "/Users/user/anaconda/lib/python3.4/site.py", line 506, in execsitecustomize
import sitecustomize
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/usr/local/lib/python2.7/site-packages/sitecustomize.py", line 15, in <module>
str(sys.version_info[0]) + '.x!\n PYTHONPATH is currently: "' + str(os.environ['PYTHONPATH']) + '"\n' +
File "/Users/user/anaconda/lib/python3.4/os.py", line 633, in __getitem__
raise KeyError(key) from None
KeyError: 'PYTHONPATH'
# destroy sitecustomize
I have looked around and found that 'PYTHONPATH' does not exist as a key in os.environ.
If your PYTHONPATH environment variable is set, unset it. You can check with echo $PYTHONPATH. If it is set it is probably coming from something in ~/.profile or ~/.bash_profile.
The issue is the file /usr/local/lib/python2.7/site-packages/sitecustomize.py. You may want to check what that file is and where it comes from, but removing it should fix the problem.
Going to necro-answer here with more detail for folks that might hit this page after searching for the error shown…
If your Mac has messages referencing /usr/local/, I'm going to go ahead and assume you've used homebrew to install something. In this case, Python.
When Anaconda's Python distribution is installed, one of the things it'll check is if there are any site customizations applied to your existing Python installation. If you installed any version of Python using Homebrew, you likely have such a site customization.
Running conda info -a | grep dirs will get your Anaconda install info and look for a line with dirs included. Only one should match, if it exists:
user site dirs: ~/.local/lib/python3.5
If it does exist, cd to that directory (whatever it is), and get a directory listing (ls). You'll then (likely) find a file called homebrew.pth.
Remove that file, and the error goes away.
Reason: Anaconda is referencing that homebrew.pth file, which then goes on to include the sitecustomize.py from your earlier homebrew-installed version of Python.