0

Problem: I would like to import a module (name: Ex3.2_myModule) to a file (name: Ex3.2_Test.py). They are in the same directory. To do it I tried several options (i.e. screenshot) proposed in several other discussions on this topic (i.e. Discussion 1 and Discussion 2). Neither of them work.

Screenshot: 1: https://i.stack.imgur.com/GnFQg.png

Question: How can I import this module without changing the name of the module?

Code:

Ex3.2_Test.py

# 1
import Ex3.2_myModule as mm

#2
mm = __import__("Ex3.2_myModule")

#3
import importlib
mod = importlib.import_module("Ex3.2_myModule")


mm.helloWorld()

Ex3.2_myModule.py

def helloWorld():
    print("Hello, World")

Solution: The solution proposed in Discussion 2 works after all. Thanks a lot to @a_guest for pointing it out!

PavelNikov
  • 48
  • 6
  • Could you please provide the code in your post and not in the screenshot. – tjallo Jun 30 '20 at 13:08
  • What does "import a module to a file" mean? If you mean to import a module *from* a file, you'll have to change the name of the file, or use the `importlib` module. The `import` statement treats the `.` as separating a package name from a contained module name, not as part of a single module name. – chepner Jun 30 '20 at 13:12
  • @tjallo: Thanks for your input! – PavelNikov Jun 30 '20 at 13:21
  • @chepner: Might be a silly question, but isn't Ex3.2_Test.py a file where I import the module? – PavelNikov Jun 30 '20 at 13:23
  • Does this answer your question? [How to import a module given the full path?](https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path) – a_guest Jun 30 '20 at 13:28
  • @a_guest Unfortunately not. Just tried it – PavelNikov Jun 30 '20 at 13:36
  • @Pewepow Why not, what's the issue with that answer? It should work. What version of Python do you use? – a_guest Jun 30 '20 at 13:38
  • @a_guest Now it worked. First time it didn't work because of complications with the path. Thanks for your help! I really appreciate it! Is this post a duplicate now? I had looked at the post you linked before (in fact I linked it in my question) but didn't realize that it would solve my problem since the the topics are at first glance not identical. – PavelNikov Jun 30 '20 at 14:01

3 Answers3

2

The situation is tricky, because dots mean subpackage structure to python. Never the less it is still kinda possible with imp:

import imp
mm = imp.load_source('mm', 'Ex3.2_myModule.py')

mm.helloWorld()

note: this is unorthodox and you are recommend to simply rename your modules instead.

Seeing as this is probably for an excersise, you should just rename your modules.

tjallo
  • 781
  • 6
  • 25
  • The [`imp`](https://docs.python.org/3/library/imp.html) module is deprecated since Python 3.4. – a_guest Jun 30 '20 at 13:29
  • Unfortunately this doesn't work for me. Where should I post the error this gives me? – PavelNikov Jun 30 '20 at 13:31
  • @Pewepow Is this for some kind of (school) exercise? (Because of the naming Ex3.2) If so please just change the name. If they give you a hard time about the name change, just explain to them that a decimal point in modules is for subpackages. Using them in your module name is inproper, and unpythonic, it's bad practice. They are literally teaching you bad practice, if they give you a hard time. I know this isn't the answer to your question. But I think it is the answer to your problem! ;) – tjallo Jun 30 '20 at 13:47
  • @tjallo You are right. It is bad practice and I will change the file name. However, I'd still like to know if there is a way to make it work with the current name. I might be a bit stubborn I have to admit :) – PavelNikov Jun 30 '20 at 13:53
0

The following stolen from https://stackoverflow.com/a/43602557/5386938 works on my machine

from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader 

spec = spec_from_loader("Ex3.2_myModule", SourceFileLoader("Ex3.2_myModule", ".\\Ex3.2_myModule.py"))
mm = module_from_spec(spec)
spec.loader.exec_module(mm)

mm.hello()
help(mm)

where the contents of Ex3.2_myModule.py is

def hello():
    print('Hello, World!')

0
import imp
with open('Ex3.2_myModule.py', 'rb') as fp:
    mm = imp.load_module('models_admin', fp, 'models.admin.py', ('.py', 'rb', imp.PY_SOURCE) )
mm.helloWorld()
Ronald
  • 2,930
  • 2
  • 7
  • 18