NameError: name 'reload' is not defined python 3.6.4 - python

After installing Xadmin, I encounter some problems. These are my error details:
[File "C:\Users\Harry\PycharmProjects\mxonline\lib\site-packages\xadmin\sites.py", line 9, in <module>
reload(sys)
NameError: name 'reload' is not defined][1]
I've tried to import importlib importlib.reload(sys) but it still doesn't work. I am using python 3.6.4.

Assuming that I understand the problem, you are having issues with importing reload from the package importlib and you encounter the NameError when trying to use reload.
If this is all correct, then make sure you are importing reload correctly. If you just want reload try:
from importlib import reload
This will import reload while making it available under the name reload. If you want to give it an alias you could do:
from importlib import reload as foo
Finally, if you needed all of importlib you could also import the package as an alias:
import importlib as il
And then use reload from this like so:
il.reload(sys)

This code is doing something super duper weird and incompatible with Python 3. Importing reload from importlib will not help, even if you were to edit the library's code to import reload, because it is also relying on sys.setdefaultencoding, which does not exist on Python 3.
To use this code on Python 3, you would have to install an updated version directly from github, since the most recent release doesn't have the fix. I don't know whether the code has other issues with Python 3, though. Personally, I would probably not use xadmin at all.

Related

import from lib in python

I import vtt_to_srt converter.I read the instructions.I make the installation.From beginning to this "no problem" but when I import it as manuals describe python can't find the module there.
Installation
pip install vtt_to_srt3
Usage
from vtt_to_srt import vtt_to_srt
path = '/path/to/file.vtt'
vtt_to_srt(path)
Error
ImportError: cannot import name 'vtt_to_srt' from 'vtt_to_srt'
I am a rookie.Sorry for asking this question.
#SakuraFreak's answer was my immediate thought, too, but I ended up investigating this a bit further. It seems like vtt_to_srt stores all its API in the __main__.py file of the module. This file should normally be used when you want to a module using python -m.
This actually makes the module impossible to use the way that the documentation specifies. What I tried, then was:
from vtt_to_srt.__main__ import vtt_to_srt
print(vtt_to_srt)
This results in:
<function vtt_to_srt at 0x000002A0948216A8>
So it seems like this workaround is OK.
I do not know if storing all the module's code in __main__.py is some convention not supported by my version of Python (CPython 3.7.3 on Windows), or if it simply an error. Maybe you should approach the module's owners with this.
Because you're trying to import a function from the module which doesn't exist.
the correct way for this module is:
import vtt_to_srt

ImportError python module

I have a python module that I have added a new file to. When I try to import the new file as
from my_module.new_file import new_class
it throws up an import error, where as the same thing works fine for the original files that were in there.
I'm sure this is down to the module being cached somewhere, and not updating the cache (if I remove the module folder completely importing the original code still works).
Can someone tell me where python 3.6 caches modules, how to remove the cached module, or force python to load the new file into the module.
Found the answer:
import importlib
importlib.reload(my_module)

Override installed module in import

I am currently working on a package that's nearly ready for distribution. As such, I'm trying out installing it (via setup.py) on my system. However, I also need access to the current (source code) package, e.g. for tests.
Is it possible to explicitly force importing the local package instead of the installed version? What I already tried is adding the path at the beginning of sys.paths, with no luck.
I guess this question goes into how exactly Python looks for the modules to import, and how to change the order.
EDIT : It was a silly mistake, adding at the beginning of sys.paths works. I had some other import statement elsewhere that was executed first.
This is bad bad idea to do something like force importing the local package.
Python3 always import the built-in modules first, and obviously you're using Python3 cause Python2 is local-first.
I don't know how to solve this problem, if I were you I'd rename that file.
edit
You don't use append cause append is to the end. Use insert.
Tested on my cpu. I created a requests.py in another folder.
>>> import sys
>>> sys.path.insert(0 ,"path-to-another-folder/")
>>> import requests
>>> requests.get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'get'
>>>
If I don't do sys.path.insert(0 ,"path-to-another-folder/"), requests is imported normally.

ImportError: cannot import name defaultdict

I'm getting this really strange ImportError when running from collections import defaultdict:
ImportError: cannot import name defaultdict
I'm running python 2.7, and the strange part is that in other parts of my application this exact same import line succeeds.
I thought maybe that's a circular import, but it doesn't make much sense when it comes to built-in python modules.
Any ideas why I get this error?
You probably have a module named 'collections' in your project.
Try renaming this module in your project.
Check that you have your own version of collections.py in python module search path.
It will prevent importing of the standard module collections.
You can confirm that by using following statements:
import collections
print(collections) # => This will print the module information. (esp. path)
most probably you have name clashes.
Try to rename your module or method.
In Python Programming Standarts, Python puts underscore for name clashes you can also do that(http://legacy.python.org/dev/peps/pep-0008/#function-and-method-arguments).

Why doesn't import work for me? - Python

Whenever I try to import a file into python, it comes up with this error(or similar):
Traceback (most recent call last):
File "C:/Python33/My Files/username save.py", line 1, in <module>
import keyring.py
ImportError: No module named 'keyring'
I am trying to create a password storing program, and I was looking up for good ways to keep passwords secure, and someone said use import keyring, so I did, except, it never works. I must be doing something wrong, but whenever I look anything up for python, it never works out for me. It's almost as if loads have things have been changed over the years.
and idea's?
The keyring module is not part of the Python standard library. You need to install it first. Installation instructions are included.
Once installed, use import keyring, not import keyring.py; the latter means import the py module from the keyring package. Python imports should use just the name of the module, so not the filename with extension. Python can import code from more than just .py python files.
I was getting the same error "ModuleNotFoundError: No module named 'keyring'". And after installing this module pip install keyring, the same error occured with another module name. Then I came to the conclusion that it is the fact that the VSCode is not able to connect to my venv, even after setting the Python Itereptor. Press CTRL + SHIFT + P, and then type Python: Select Interceptor, and select your venv, if you want to set this.
To fix the issue, I had to force the VSCode to use the .venv I created, and luckily there is a dropdown to do that, on the top right corner as in the preceeding image. Click ont the Python version, and then you will be able to select your virtual environment.
Now it will take the modules from your virtual environment.

Categories