Can someone explain this odd Python/Django import behaviour?
Date : March 29 2020, 07:55 AM
Hope this helps When you open a Django shell, it adds the path to your project to sys.path. You can see this by running import sys; print sys.path on a Django shell and on a normal python shell and comparing the output. You will note that the output from the Django shell includes the path to the mywebsite directory as the first item of the list. Basically, it means that the two imports create two different module objects, since they are gotten from different points in the search path. The comparison check returns False since the module objects have different id's (memory address) # These two values will be different
id(mywebsite.myapp.models)
id(myapp.models)
|
Strange Python import behaviour
Date : March 29 2020, 07:55 AM
will help you I have a problem with importing sub-packages. The packages structure I have is: import project.helpers
project.helpers.somefunction()
|
Unexpected import behaviour in Python 3
Date : March 29 2020, 07:55 AM
hop of those help? This problem was the result of several configuration issues. I had marked all of my apps as source folders in Pycharm. I had also selected to "Add source roots to PYTHONPATH" in my run configuration.
|
Python unexpected import behaviour
Date : March 29 2020, 07:55 AM
help you fix your problem Based on the comments on the original post I made the following changes to my code: instead of importing classes on installed_io module I used strings: #installed_io.py
forms = [("json","JsonForm"),("csv","CsvForm"),....]
from installed_io import installed_inputs
def get_input_form(slug):
for entry in installed_inputs:
if entry[0] == slug:
module = importlib.import_module("tensorflow_board_django.io.forms")
class_name = entry[1]
return getattr(module, class_name)
raise NotImplementedError("The required form is not implemented or missing from the installed inputs")
|
Is it good practice to load large objects in one file and import them in other Python files?
Tag : python , By : Christopher
Date : March 29 2020, 07:55 AM
To fix the issue you can do Couple of considerations. So firstly, anything that's just flat in a file will be executed when the file is imported, which is usually immediately. So I often do my loading upfront in my files. But it doesn't have to be in one location. You can do this in many different files. For example: Slow: from nltk.corpus import words
def check_word(word):
return word in words.words(): # gets executed on every call
from nltk.corpus import words
WORDS = frozenset(words.words()) # Only gets executed once
def check_word(word):
return word in WORDS
# Inefficient memory usage
import pandas as pd
df_1 = pd.read_csv('first.csv')
df_2 = pd.read_csv('second.csv')
do_something(df_1)
do_something(df_2)
df_1.to_csv('output1.csv')
df_2.to_csv('output2.csv')
# Efficient memory usage
df_1 = pd.read_csv('first.csv')
do_something(df_1)
df_1.to_csv('output1.csv') # Last reference to df_1. dropped from memory here
df_2 = read_csv('second.csv') # Only df_2 in memory now
do_something(df_2)
df_2.to_csv('output2.csv')
|