I'm trying to get my head around Python modules. I currently have the current setup. My main code in file test.py:
from superpackage import supermodule
supermodule.woww()
I then have a directory named superpackage inside this folder is the module file supermodule.py defined as so:
def woww():
print "wow"
In the same directory I have a blank __init__.py.
The code executes. Is there a way to import the contents of the package so I can just call woww() without the supermodule.? When working with just modules I can do from supermodule import woww, but is there an equivalent for packages?
Related
I am currently trying to import some python files inside of a folder named "modules". My file structure is as follows:
src
- classes
- modules
- image generator ( the file where I am trying to import modules)
Error:
from modules.processing import StableDiffusionProcessingTxt2Img, process_images
ModuleNotFoundError: No module named 'modules'
from modules.processing import StableDiffusionProcessingTxt2Img, process_images
I have attempted to add both an __init__.py in my classes folder, as well as my modules folder, but unfortunately that did not resolve any of my problems.
I have verified that the files I am trying to import are infact in my modules folder.
You must import from the projects' root directory. Assuming src/ is the root directory, you must import as follows:
from classes.modules.processing import StableDiffusionProcessingTxt2Img, process_images
Since you are trying to import functions from another python file that's located on another folder, it is having a hard time figuring out where it is located.
Try this:
import sys
sys.path.insert(1,'/src/classes/modules')
import (your python file)
Make sure you keep the above code on the python file where you are trying to import functions from other .py files
Remember, the path is the absolute path from root. Not sure how many more layers you have before src, put accordingly.
Also, the last line is import python file. So, if you have x.py inside modules, try import x. Then you will be able to use functions inside there.
I ran into some trouble with modules lately. I'm having a hard timefiguring out how I can get the file that has imported the module. basically:
import sys
imported = sys.get_modules_that_have_imported_this_script()
#and then the rest of the code
to be more specific. modules run when you import them right? so is there a way to get what program the module is running from? (preferably the path or file name)
edit: i want the file name so I can use the inspect module to get source code
edit2: I don't want to get the source of the module. I want to find the main program from a module imported within it
edit3: maybe this will help
https://pasteboard.co/OMO6tQoTKALI.png
I found the answer in here How to get filename of the __main__ module in Python?
If you have two files main.py and module.py, this is how you can find the path of main.py from module.py
main.py
import module
module.function()
module.py
import __main__
main_filename = __main__.__file__
def function():
...
This question already has answers here:
Import Script from a Parent Directory
(3 answers)
Closed 1 year ago.
File Organization
./helper.py
./caller1.py
./sub_folder/caller2.py
It's fine when importing helper.py from caller1.py.
caller1.py
from helper import hello
if __name__ == '__main__':
hello()
but when importing from ./sub_folder/caller2.py with the exact same code I got the error...
ModuleNotFoundError: No module named 'helper'
I would like to organize files into sub-folders because the project is quite large.
As a solution for this,
You can add the path of that file in system using sys module and then you can import that perticular file.
like in your case
caller2.py
import sys
sys.path.insert(0, "C:/Users/D_Gamer/Desktop/pyProject/helperDir")
# You need to provide path to the directory in which you have a helper file so basically I have "helper.py" file in "helperDir" Folder
from helper import hello
if __name__ == '__main__':
hello()
There are other ways to achieve the same but for now you can hold onto this and
for more information checkout this reference on Github
You have to understand how Python finds the modules and packages when using import.
When you execute a python code, the folder in which this file is will be added to the PYTHONPATH which is the list of all the places where python will look for your packages and modules.
When you call caller1.py, it works because helper.py is in the same folder but it does not work for caller2.py because python does not find it in the same folder nor the other path in PYTHONPATH.
You have three options:
call caller2.py from a script in the same folder as helper
add the folder containing helper.py in the PYTHONPATH (not recommended)
make your code into a package that you can pip install -e, this way python will be able to find your modules as they will be install in the site-packages of your python environment (most complex but cleanest solution)
I have created a folder named C:\Python27\Lib\site-packages\perso and inside I have put a file mymodule.py. The goal is to have this module accessible from any future Python script.
Let's do a D:\My Documents\test.py file:
import mymodule #fails
import perso.mymodule #fails
Why does it fail? How to import a module from C:\Python27\Lib\site-packages\perso? What are the best practice for using user-modules in all Python scripts of the same computer?
check PythonPath
create __init__.py to use perso as package
Python Modules:
In order to create a module you can do the following:
under <Python_DIR>\Lib\site-packages:
put you directory <module>. This directory contains your classes.
In <Python_DIR>\Lib\site-packages\<module> put an init file __init__.py,
This file define what's in the directory, and may apply some logic if needed.
for example:
__all__ = ["class_1", "class_2",].
Than, to import:
from <your_module> import <your_class>
For more information, read this.
I'm totally new to Python, and I want to create my first Python library for peronal uses.
I'm using Python 2.7.5, and running the IDLE interface.
So far, what I understood from the documentation and related questions is that:
Python goes through a list of directories listed in sys.path to find scripts and libraries
The package directory must contain a __init__.py file, which can be empty
The module I want to create should be a modulename.py file with the code inside the package directory
(Sources: http://www.johnny-lin.com/cdat_tips/tips_pylang/path.html --- https://docs.python.org/2/tutorial/modules.html)
And here is what I tried that fails:
Created a personal package directory C:\....\pythonlibs
Created a subpackage dir C:\....\pythonlibs\package
Created the __init__.py file inside both folders
Created the mymodule.py file in the packacge dir
And then in the IDLE used this code:
import sys
sys.path.append(r'C:\....\pythonlibs')
First issue:
Currently I have to do this append every time I enter the IDLE. How can I keep the directory in sys.path permanently just as there are a lot of other directories there?
Then I tried importing my package:
import pythonlibs #fails!! why?
import pythonlibs.package #fails!! why?
import package #works
The error is: ImportError: No module named pythonlibs
Second issue?
This seems to be against the documentation, why can't I import from the root pythonlibs folder?
With line
sys.path.append(r'C:\....\pythonlibs')
you are instructing interpreter to start looking for modules (libraries) in this directory. Since this directory does not contain pythonlibs folder (the parent does), it can't import it.
Similarly - because it contains the module package, it can import it.