Why this reload fails with NameError: name xxx is not defined?
Credits to https://stackoverflow.com/a/46814062/237105.
# Reload via sys.modules as it is not imported directly
reload(sys.modules['foo_module'])
# Then, reimport function
from foo_module import foo
Total code of yours, fixed:
import sys
from time import sleep
from importlib import reload
from foo_module import foo # <-- import specific item only
print(sys.modules['foo_module'])
while True:
foo()
# simulate a delay or a condition by when foo_module would have changed
sleep(2)
# reload foo_module via sys.modules as it is not imported directly
reload(sys.modules['foo_module'])
# re-import foo to update it.
from foo_module import foo
Python3 - reload() can not be called on __import__ object?
The reload
built-in function has been moved to importlib
module in Python 3.4:
In [18]: from importlib import reload
In [19]: reload?
Reload the module and return it.
The module must have been successfully imported before.
As pointed out by @JPaget in comments reload()
function has been moved from imp
to importlib
module in Python 3.4+. From what's new in Python 3.4:
The
reload()
function has been moved fromimp
toimportlib
as part of
theimp
module deprecation
Web scraping python error (NameError: name 'reload' is not defined)
reload is not supported in Python3 anymore
You should remove these lines
reload(sys)
sys.setdefaultencoding('utf8')
Instead you should pass encoding='utf-8'
as arguement when open file in Python3.x
Line no 29:
with open('/home/l/Downloads/WebScraping/GoodReadsBooksNew.csv', 'w+') as csv_file:
changes to
with open('/home/l/Downloads/WebScraping/GoodReadsBooksNew.csv', 'w+', encoding='utf-8') as csv_file:
Line no 34:
lines = [line.rstrip('\n') for line in open('/home/l/Downloads/WebScraping/BookTitles.txt')]
changes to
lines = [line.rstrip('\n') for line in open('/home/l/Downloads/WebScraping/BookTitles.txt', encoding='utf-8')]
Line no 62:
with open('/home/l/Downloads/WebScraping/GoodReadsBooksNew.csv', 'a') as csv_file:
changes to
with open('/home/l/Downloads/WebScraping/GoodReadsBooksNew.csv', 'a', encoding='utf-8') as csv_file:
Reload a Module in Python 3.4
The imp
module was deprecated in Python 3.4 in favor of the importlib
module. From the documentation for the imp
module:
Deprecated since version 3.4: The
imp
package is pending deprecation
in favor ofimportlib
.
So, you should be using the reload
function from there:
>>> import importlib
>>> importlib.reload
<function reload at 0x01BA4030>
>>> importlib.reload(the_module)
Why do I get NameError: name '_' is not defined when setting custom templates for djangocms-video?
In Django, the gettext_lazy(…)
function [Django-doc] is often imported as _
to manage translations. This is explained in the Standard translation:
Python’s standard library
gettext
module installs_()
into the global namespace, as an alias forgettext()
. In Django, we have chosen not to follow this practice, for a couple of reasons(…)
Because of how xgettext (used by makemessages) works, only functions that take a single string argument can be imported as
_
:
gettext()
gettext_lazy()
You thus should add:
from django.utils.translation import gettext_lazy as _
at the top of the file.
If you thus define something like _('Featured Version')
, you can run makemessages
to generate translation files and fill in the translation per language.
having an error in my code NameError: name 'tweets_df' is not defined
You might creating a list of dataframes, then using the pandas.concat method to concatenate all list of dataframes into a single dataframe, something like
tweets_df_list = []
for tweet in tqdm(tweets_copy):
hashtags = []
try:
for hashtag in tweet.entities["hashtags"]:
hashtags.append(hashtag["text"])
except:
pass
tweets_df_list.append(pd.DataFrame({'user_name': tweet.user.name,
'user_location': tweet.user.location,\
'user_description': tweet.user.description,
'user_created': tweet.user.created_at,
'user_followers': tweet.user.followers_count,
'user_friends': tweet.user.friends_count,
'user_favourites': tweet.user.favourites_count,
'user_verified': tweet.user.verified,
'date': tweet.created_at,
'text': tweet.text,
'hashtags': [hashtags if hashtags else None],
'source': tweet.source,
'is_retweet': tweet.retweeted}, index=[0]))
tweet_df = pd.concat(tweets_df_list)
tweets_df.to_csv('newData.csv',index=False)
Related Topics
How to Have Assignment in a Condition
Passing Table Name as a Parameter in Psycopg2
Problem with Multi Threaded Python App and Socket Connections
Python: Pandas Series - Why Use Loc
Custom Filter in Django Admin on Django 1.3 or Below
Reading a Text File and Splitting It into Single Words in Python
Pycharm Doesn't Recognise Installed Module
Splitting a List Based on a Delimiter Word
Python: Urlerror: <Urlopen Error [Errno 10060]
Google Colab: How to Read Data from My Google Drive
How to Profile Python Code Line-By-Line
How to Change Default Anaconda Python Environment
How to Get an Event Callback When a Tkinter Entry Widget Is Modified
Importerror: No Module Named _Ssl