Asked  7 Months ago    Answers:  5   Viewed   39 times

Could someone provide me with a good way of importing a whole directory of modules?
I have a structure like this:

/Foo
    bar.py
    spam.py
    eggs.py

I tried just converting it to a package by adding __init__.py and doing from Foo import * but it didn't work the way I had hoped.

 Answers

32

List all python (.py) files in the current folder and put them as __all__ variable in __init__.py

from os.path import dirname, basename, isfile, join
import glob
modules = glob.glob(join(dirname(__file__), "*.py"))
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]
Tuesday, June 1, 2021
 
erotsppa
answered 7 Months ago
100

Not sure if it's the best way to do it but here's what I came up with:

(Only tested on .Net Core RC2 - Windows)

using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using Microsoft.Extensions.DependencyModel;

namespace AssemblyLoadingDynamic
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var asl = new AssemblyLoader();
            var asm = asl.LoadFromAssemblyPath(@"C:LocationOf" + "SampleClassLib.dll");

            var type = asm.GetType("MyClassLib.SampleClasses.Sample");
            dynamic obj = Activator.CreateInstance(type);
            Console.WriteLine(obj.SayHello("John Doe"));
        }

        public class AssemblyLoader : AssemblyLoadContext
        {
            // Not exactly sure about this
            protected override Assembly Load(AssemblyName assemblyName)
            {
                var deps = DependencyContext.Default;
                var res = deps.CompileLibraries.Where(d => d.Name.Contains(assemblyName.Name)).ToList();
                var assembly = Assembly.Load(new AssemblyName(res.First().Name));
                return assembly;
            }
        }
    }
}

MyClassLib.SampleClasses is the namespace and Sample is the type aka class name.

When executed, it will try to load the SampleClassLib.dll compiled class library in the memory and gives my console app access to MyClassLib.SampleClasses.Sample (take a look at the question) then my app calls the method SayHello() and passes "John Doe" as name to it, Therefore the program prints:

"Hello John Doe"

Quick note: The override for the method Load doesn't matter so you might as well just replace its content with throw new NotImplementedException() and it shouldn't affect anything we care about.

Thursday, June 10, 2021
 
cusejuice
answered 6 Months ago
12

If Y is a module (and X a package) reload(Y) will be fine -- otherwise, you'll see why good Python style guides (such as my employer's) say to never import anything except a module (this is one out of many great reasons -- yet people still keep importing functions and classes directly, no matter how much I explain that it's not a good idea;-).

Monday, June 21, 2021
 
SuperString
answered 6 Months ago
28

shaders is a submodule, not a function.

The syntax from module import something doesn't import submodules (Which, as another answer stated, not defined in __all__).

To take the module, you'll have to import it specifically:

from OpenGL.GL import shaders

Or, if you only want to have a few functions of shaders:

from OpenGL.Gl.shaders import function1, function2, function3

And if you want to have all the functions of shaders, use:

from OpenGL.Gl.shaders import *

Hope this helps!

Tuesday, August 3, 2021
 
Shreejibawa
answered 4 Months ago
79

You can use the raw ftp commands rnfr and rnto. rnfr chooses the file to rename from. After receiving a confirmation you can use rnto to store it under a different filename. this works also with directories. You will have to be at a parent directory in order to that that and create to directory to move to as well, for example:

to move /rootFolder/1 to /rootFolder/2 :

CWD /rootFolder
MKD 2
RNFR 1
RNTO 2
Tuesday, November 23, 2021
 
Stephan
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share