Override installed module in import - python

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.

Related

Python module not getting deleted properly

Somehow I ended up installing 2 different modules named 'google'. One is present in site_packages folder inside python, and the one in my custom path provided in PYTHONPATH.
I wanted to get rid of the one in site_packages. So I deleted that folder (as per other SO answers). But now, I am facing this weird scenario that it still loads the wrong on at start
>>> import google; google.__path__
['/Library/Python/2.7/site-packages/google']
>>> google.__file__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
>>> reload(google)
<module 'google' from '/custom_path/google-cloud-sdk/platform/google_appengine/google/__init__.pyc'>
>>> google.__path__
['/custom_path/google-cloud-sdk/platform/google_appengine/google'
>>> google.__file__
'/custom_path/google-cloud-sdk/platform/google_appengine/google/__init__.pyc'
So basically, first time it's still loading the old module (with no files in it), and on reloading, it's giving the correct one.
My question is how to delete the old one completely as it's not feasible to always reload module like this.
The problem was that somehow other modules were holding a reference to the google module. I removed all packages from google and re-installed them and it worked perfectly after that. I am not yet sure why & how the module was still being referenced, so would appreciate if anyone can throw light to that as well.

ImportError: No module named stdio, any way to start fresh with python?

I'm getting the error
Traceback (most recent call last):
File "ghs.py", line 1, in <module>
import stdio
ImportError: No module named stdio
When I try to run my script. I can run my script on other machines just fine. I have installed python using homebrew. And I've tried everything I can think of to get it to recognize my modules! I've uninstalled and reinstalled using brew. I've tried changing the path (though I don't fully understand this). I get no issues using brew doctor.
I've also tried using a python virtual environment but to no avail.
Any ideas on how to fix this issue or else 'start fresh' from a fresh version of python?
When you import a module, Python looks for it at the directory your code is, and the directory in which the built-in libraries are (C:\Users\pc\AppData\Local\Programs\Python\Python35-32\Lib in my case, I'm using Windows 10 and Python 3.5). If it can't find it, it raises ImportError.
I couldn't find a module named stdio in my computer. I also know some C++ and as far as I know, stdio is the library for inputs and outputs(prints). In python, there is no need to import such a library.
You can use try,except statement to test if your code works without importing the module like this.
try:
import stdio
except:
#rest of your code goes here
You will need to indent your whole code however this can be done easily with a text editor in which you can edit more than one line at a time.

How to fix error "AttributeError: 'module' object has no attribute 'client' in python3?

The following is my code.
import http
h1 = http.client.HTTPConnection('www.bing.com')
I think it's ok.But python give me the following error:
AttributeError: 'module' object has no attribute 'client'.
I wanted to know why and how to fix it.Thanks.
First, importing a package doesn't automatically import all of its submodules.*
So try this:
import http.client
If that doesn't work, then most likely you've got a file named http.py, or a directory named http, somewhere else on your sys.path (most likely the current directory). You can check that pretty easily:
import http
http.__file__
That should give some directory like /usr/lib/python3.3/http/__init__.py or /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/__init__.py or something else that looks obviously system-y and stdlib-y; if you instead get /home/me/src/myproject/http.py, this is your problem. Fix it by renaming your module so it doesn't have the same name as a stdlib module you want to use.
If that's not the problem, then you may have a broken Python installation, or two Python installations that are confusing each other. The most common cause of this is that installing your second Python edited your PYTHONPATH environment variable, but your first Python is still the one that gets run when you just type python.
* But sometimes it does. It depends on the module. And sometimes you can't tell whether something is a package with non-module members (like http), or a module with submodules (os). Fortunately, it doesn't matter; it's always save to import os.path or import http.client, whether it's necessary or not.

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.

Python: can not import util

While reading the code for django/forms/widgets.py, I saw:
from util import flatatt
To dig deeper, I tried to import the util module in a Python shell in my terminal, but I got an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named util
This left me confused. What is wrong?
Django is (ab?)using Python's import system in order to import something from django.forms.util. Import that module instead.
In python, you get this ImportError:
Traceback (most recent call last):
File "b.py", line 3, in <module>
from util import flatatt
ImportError: No module named util
Some programmer out there created a library for your python script to use. Your program was unable to find that library. So the interpreter tells you that it can't be found. You have a few options. Either you have to define the library and functionality yourself, or you'll have to review the instructions and source code of the program you are trying to run, and try to figure out what is broken to prevent the library from being included. Or the final option is to remove any usage of that library in the current script.
You can define the module yourself like this:
Put this in util.py in the same directory as your widgets.py file.
def flatatt(prompt):
print("ok")
If you define that, you will get a different error, the script will import your library, and find that method, but then will do the wrong thing, because the programmer who designed that code probably had some other functionality in mind.
You need to figure out what you did wrong, or what the original programmer did wrong, or how your specific system is different to cause this to work.
You can learn more about what I've described above here, by rolling your own python modules: Python: How to import other Python files
Often times these sorts of bugs are barriers to entry, the original developers don't want programmers who don't know the difference between an integer and a module using the software. So little problems like this are added, to help encourage you that you need to develop a better understanding of the source code under the hood. It's like a mechanic helping everyone become better mechanics by swapping the wires on the distributor cap. It isn't going to work until you swap them back rightly.
from django.forms.utils import flatatt
It worked with django 1.11 version, and may work with 1.8 through 1.10

Categories