Differences between Local and Global importing in Python

Is there a way to force my interpreter (2.7) to import a module from site packages in case of a conflict? Let's say you are working with a directory structure like this:

top_level
----cool_mod
    ----init.py
    ----sweet_module.py

You already have sweet_module installed in the site packages. When running Python from this specific directory, if you use:

from cool_mod.sweet_module import *

You will import the local module instead of the global one. Is there any way to change this behavior?

This issue may come up in scenarios like:

top_level

setup.py
----cool_mod
    ----init.py
    ----sweet_module.py

Before installation, you can run cool_mod.sweet_module when your working directory is top_level. After installation, you should be able to import cool_mod.sweet_module from anywhere except this specific directory. However, even after installation, if you import from this directory, you will still get the local copy.

Answer №1

To add the site package directory to the beginning of sys.path, import it afterwards.

Alternatively, you can utilize imp.load_source to import a module from a specified path.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Challenges in the world of Dungeons & Dragons gaming

This is the main function that will execute everything import GameClass def callMain(): game = GameClass game.Run() #GameClass.Run() callMain() Upon executing the main function that triggers everything, an error occurred:" Traceback (most ...

Locate element by xpath in Python with the help of Selenium WebDriver

Is it possible to locate these elements using xpath? <a class="single_like_button btn3-wrap" onclick="openFbLWin_2224175();"> <span>&nbsp;</span><div class="btn3">Like</div></a> and <button value="1" class="_42 ...

What is preventing selenium from locating the chrome driver?

As I was following a tutorial on creating a web scraper for Twitter using Selenium and Python, I encountered an issue. File "C:\Python34\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __i ...

Exploring Python's Capabilities: File Reading

Hello, I am currently learning Python so please bear with me. I am referring to this tutorial: http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files I am attempting to read a file; here is the content of my file: # cat test line1 ...

Discovering the clickable widget index in pyqt4: A beginner's guide

In my application, I am working on creating multiple widget orders using a list of dictionaries to create a list of widgets. I have implemented a mouse release event for clickable widgets, but I am facing issues in identifying the clicked widget index. C ...

Compare the data in one column of the dataframe with the data in

Here is the data frame I am working with: CET MaxTemp MeanTemp MinTemp MaxHumidity MeanHumidity MinHumidity revenue events 0 2016-11-17 11 9 7 100 85 63 385.943800 rain 1 2016-11-18 ...

Different means of obtaining Azure AD login records in Python without relying on the subprocess library or powershell.exe

After researching various sources, it seems that the subprocess library in Python is commonly used to run PowerShell commands from within Python. For instance: data = subprocess.check_output(["powershell.exe", "Connect-AzureAD -AccountId < ...

Spacy model packaging

I am working on incorporating the spacy model de_core_news_sm into a Python package. Check out my project here: https://github.com/michaelhochleitner/package_de_core_news_sm. To package and install the project, I use the following commands: python setup ...

What is the best way to incorporate a search feature within Bootstrap cards?

I've been struggling to incorporate a search feature within my bootstrap cards. Despite trying various online methods, none have proven successful for me so far. Below is my HTML code - any assistance in implementing a search filter into my app would ...

random identifier selection using selenium's xpath and css selector algorithm

Hello, I am a newcomer to using Selenium with Python. I am facing an issue where the id, xpath, and css selector contain random values each time I navigate to the page. I have tried using various methods like xpath, id, css selector, and even class name to ...

Guide on uploading a file using PUT in Python without the need to manually open the file

Issue: 1) I am looking for a Python library that allows me to provide the path of a file instead of opening it directly (especially if the file is large) in order to make a PUT request. 2) Additionally, I need this library to handle automatic redirection ...

Pass a list item as a parameter to execute a function in Python

I need to execute a series of functions, each taking an item from a list as a parameter. Func1 will take the first list value, Func2 the second list value...and so forth. My current setup looks like this: main.py #import the 5 python files import s ...

Using BeautifulSoup to extract data from a webpage containing JavaScript

Hello everyone! I am reaching out for help once more. While I am comfortable scraping simple websites with tags, I recently came across a more complex website that includes JavaScript. Specifically, I am looking to extract all the estimates located at the ...

What is the best way to increase the duration of drawing lines in tkinter in order to create a smooth animation

I'm dabbling with lines in tkinter as I work on creating a "random walk" application. While I can draw lines on a canvas, I aim to introduce a brief pause between each line that is drawn to give the effect of an animation rather than just an instantan ...

Typing in a tkinter entry box effortlessly without the need to first click on it

Every time I launch my tkinter program, I find myself having to click on the entry box before I can start typing. Is there a way to configure it so that I can type without the need to click on the entry box? Thanks in advance. ...

Modify Pycharm key bindings for Emacs to utilize an alternative word delimiter

When I'm at work, I'm limited to using basic vi/emacs for a variety of reasons. Since I prefer emacs, I make sure to set my IDEs with emacs key bindings when I use heavier tools like Pycharm in my free time at home. I've noticed that most o ...

Changing a single string column into an array of strings with a predetermined length in a pandas dataframe

I am working with a pandas dataframe that has multiple columns. My goal is to transform one of the string columns into an array of strings with a fixed length. This is how the current table is structured: +-----+--------------------+--------------------+ ...

Working with Namespaces in LXML: How to Replace Elements in a String

Is there a way to seamlessly substitute a specific element with a snippet of string while preserving the original namespaces? <!-- language: python --> from lxml import etree replacestring = '''<AP_PTO gml:id="DEAL123456789abc"> ...

The process of "encrypting" a message using a specific set of guidelines often yields an unfinished outcome

I need to develop a function that encrypts a user's message and returns the encrypted string based on specific rules: Alphabetic Uppercase Character [A-Z]: Convert to lowercase and add a ^ character at the end Alphabetic Lowercase Character [a-z]: R ...

What is the process for accessing information from Symantec through their API?

Experiencing a 400 status code error currently. Feeling a bit stuck on what steps to take next. The 400 status code typically relates to syntax issues. How can I properly format my output into JSON file structure? import requests, json, urllib3 urllib3.d ...