logo
down
shadow

python import behaviour: different objects from same file?


python import behaviour: different objects from same file?

Content Index :

python import behaviour: different objects from same file?
Tag : python , By : wiznick
Date : November 24 2020, 05:44 AM


Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Can someone explain this odd Python/Django import behaviour?


Tag : python , By : lamberms
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


Tag : python , By : jazzyfox
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


Tag : python , By : Ray
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


Tag : python , By : S Hall
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')
Related Posts Related QUESTIONS :
  • logout a user from the system using a function in python
  • mp4 metadata not found but exists
  • Django: QuerySet with ExpressionWrapper
  • Pandas string search in list of dicts
  • Decryption from RSA encrypted string from sqlite is not the same
  • need of maximum value in int
  • a list of several tuples, how to extract the same of the first two elements in the small tuple in the large tuple
  • Display image of 2D Sinewaves in 3D
  • how to prevent a for loop from overwriting a dictionary?
  • How To Fix: RuntimeError: size mismatch in pyTorch
  • Concatenating two Pandas DataFrames while maintaining index order
  • Why does this not run into an infinite loop?
  • Python Multithreading no current event loop
  • Element Tree - Seaching for specific element value without looping
  • Ignore Nulls in pandas map dictionary
  • How do I get scrap data from web pages using beautifulsoup in python
  • Variable used, golobal or local?
  • I have a regex statement to pull all numbers out of a text file, but it only finds 77 out of the 81 numbers in the file
  • How do I create a dataframe of jobs and companies that includes hyperlinks?
  • Detect if user has clicked the 'maximized' button
  • Does flask_login automatically set the "next" argument?
  • Indents in python 3
  • How to create a pool of threads
  • Pandas giving IndexError on one dataframe but not on another similar dataframe
  • Django Rest Framework - Testing client.login doesn't login user, ret anonymous user
  • Running dag without dag file in airflow
  • Filling across a specified dimension of a numpy array
  • Python populating dataframe in pandas from text files
  • How to interpolate a single ("non-piecewise") cubic spline from a set of data points?
  • Divide 2 integers (leetcode 29) - recursion issue
  • Can someone explain why do I get this output in Python?
  • How do I scrape pdf and html from search results without obvious url
  • Is there a way to automatically make a "collage" of plots with matplotlib?
  • How to combine multiple rows in pandas with shared column values
  • How do I get LOAD_CLASSDEREF instruction after dis.dis?
  • Django - How to add items to Bootstrap dropdown?
  • Linear Regression - Does the below implementation of ridge regression finding coefficient term using gradient method is
  • How to drop all rows in pandas dataframe with negative values?
  • Most Efficient Way to Find Closest Date Between 2 Dataframes
  • Execution error when Passing arguments to a python script using os.system. The script takes sys.argv arguments
  • Looping through a function
  • Create a plot for each unique ID
  • a thread python with 'while' got another thread never start
  • Solution from SciPy solve_ivp contains oscillations for a system of first-order ODEs
  • trigger python events driven by selenium controlled browser
  • Passing line-edits to a contextmanager to set validators
  • Python: globals().items() iterations try to change a dict
  • Is it possible to specify starting values for each parameter (instead of bounds) for scipy's differential evolution?
  • why datetime.now() and constructed datetime using all fields(like year,month...) of now has big timedelta?
  • MySQL multiple table UPDATE query using sqlalchemy core?
  • find if a semantic version is superset of of another version python
  • Type checking against dynamically created objects
  • Struggling with simple reverse function
  • Is there a function for finding the midpoint of n points on sklearn.neighbors.NearestNeighbors?
  • How to set max number of tweets to fetch
  • PYTHON 3.7.4 NOT USING SQLITE 3.29.0
  • How to replace Nan value with zeros in a numpy array?
  • How to speed up calculating variance among sparse matrix
  • cupy code is not fast enough compared with numpy
  • How to count frequency of select values in Python pandas dataframe
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com