I have a folder named 'http' in my project but then the code inside the folder uses another library that uses the http library which can't be resolve because it resolves to my 'http' folder. I tried to have my __init__.py do this:
from __future__ import absolute_import
import http
But it doesn't seem to help. I am using Python3.6
I think it's not a good idea to name your module as already existing standard module. Because it makes you unable to import both within a script in your project. PEP8 recommends to append already existing standard names with underscore symbol in order to resolve such collisions. So I would recommend you to rename your module into http_.
http -> http_
PEP8 - names to avoid
Related
I tried to create my own python package (basically as described in the first 2 chapters of https://learn.datacamp.com/courses/developing-python-packages).
The folder structure of the package is like this:
mainfolder contains
a)setup.py
b)subfolder with the same name as mainfolder
subfolder contains
a)init.py (plus the 2 underscores in front and after init; somehow that is not shown here)
b) module1.py
c) module2.py
d) module3.py
e) module4.py
the only content of init.py is the import statement of all 4 modules.
I installed this package with "pip install -e setup.py".
I can successfully import either the whole package or single modules.
Sadly I have one problem I can't solve:
One of the module contains code for a function that uses requests.get() and .json() to extract data from an online API and extracts the json.
When i try to use this function in another code file (after importing the function of course) i get an error: name 'requests' is not defined.
And that is although I imported requests at the start of the code. I also can use requests normally without errors there. Only when using my custom function this error is raised.
I was only able to fix the error by rating the import requests statement INSIDE the custom function module, but that is not nice.
Somehow other packages like pandas or json work inside my custom package without importing it there.
Any ideas?
Sorry for my confusing trains of thoughts ;-)
I have searched through the existing questions on stackoverflow and wasn't able to find what I am looking for. Firstly, I am new to Python, I come from Ruby so some things seem unclear to me in Python. I learn by doing so I am writing my own python REST API client for a payment gateway, which I plan on releasing to PyPi. The problem I have is importing modules from different folders.
Let's say I have the folder structure like this:
my_project/src/lib/directory1/module1.py
my_project/src/lib/directory2/module2.py
In my_project/src/lib/directory1/module1.py I want to import a function defined in my_project/src/lib/directory2/module2.py so
# my_project/src/lib/directory2/module2.py
from lib.directory2 import module1
This doesn't work, it says ImportError: No module named directory2. I read that in Python you need to add the module to the PATH but I have gone to PyPi and took the first library from the top (SeleniumBase) to look at how the code is organised in the GitHub project and I don't see they do any stuff like that. Could you please explain to me how this works and how I can organise my codebase to be able to import modules in one module from different folders to build my own library?
I read this article https://docs.python.org/3/reference/import.html and tried what they say in section 5.7. Package Relative Imports but it doesn't work
In theory this should work
from ..subpackage2.moduleZ import eggs
But it doesn't. I get SystemError: Parent module '' not loaded, cannot perform relative import.
Import will only work if the module you are trying to import is in the same directory as the script that is doing the import.
Otherwise you have to specify the path.
Like this :
import my_project/src/lib/directory1/module1.py
I have a project containing multiple packages for example:
production/
__init__.py
prod_module_1.py
prod_subpackage/
__init__.py
prod_submodule_1.py
...
computation/
__init__.py
computation_module_1.py
...
development/
...
Now I want to prevent that specific packages can be imported by others. To go with the above example I want development modules to be able to import all types of packages but I don't want any module in production being able to import code from development. And in the same gist I want computation to be "standalone" so it can only import modules from itself, but it can be imported by other modules.
In the Python documentation https://docs.python.org/3/reference/import.html I found the following excerpt:
To selectively prevent import of some modules from a hook early on the meta path (rather than disabling the standard import system entirely), it is sufficient to raise ModuleNotFoundError directly from find_spec() instead of returning None. The latter indicates that the meta path search should continue, while raising an exception terminates it immediately.
But I'm unsure how/where I need to overwrite this method. An insight on where in the code base these changes need to be made would be greatly appreciated.
Have a look at the error in the attached scrren shot.
new is the directory which contains unittest module of python 3.6 and new2 contains unittest module of python 2.7. I understand the error raised in first case is because of missing StringIO module. But why python 2.7 is raising error despite writing the same command as in python 3. Is the syntax different in the two cases?
Does this help?:
import sys
sys.path.append('path/to/your/file')
import your.lib
You need an __init.__py file. See here: https://docs.python.org/2/tutorial/modules.html#packages
The init.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, init.py can just be an empty file, but it can also execute initialization code for the package or set the all variable, described later.
I have a module that conflicts with a built-in module. For example, a myapp.email module defined in myapp/email.py.
I can reference myapp.email anywhere in my code without issue. However, I need to reference the built-in email module from my email module.
# myapp/email.py
from email import message_from_string
It only finds itself, and therefore raises an ImportError, since myapp.email doesn't have a message_from_string method. import email causes the same issue when I try email.message_from_string.
Is there any native support to do this in Python, or am I stuck with renaming my "email" module to something more specific?
You will want to read about Absolute and Relative Imports which addresses this very problem. Use:
from __future__ import absolute_import
Using that, any unadorned package name will always refer to the top level package. You will then need to use relative imports (from .email import ...) to access your own package.
NOTE: The above from ... line needs to be put into any 2.x Python .py files above the import ... lines you're using. In Python 3.x this is the default behavior and so is no longer needed.