Python import: import module without .py extension?

In the Python system I am developing for, we usually have this module structure.

mymodule/
mymodule/mymodule/feature.py
mymodule/test/feature.py

This allows our small test framework to easily import test / feature.py and run unit tests. However, we now need some shell scripts (which are written in Python):

mymodule/
mymodule/scripts/yetanotherfeature.py
mymodule/test/yetanotherfeature.py

yetanotherfeature.py is installed by the Debian module in / usr / bin. But we obviously do not want the .py extension. So in order for the test environment to still be able to import the module, I have to make this symbolic link thingie:

mymodule/
mymodule/scripts/yetanotherfeature
mymodule/scripts/yetanotherfeature.py @ -> mymodule/scripts/yetanotherfeature
mymodule/test/yetanotherfeature.py

Is it possible to import a module by file name in Python, or can you think of a more elegant solution for this?

+3
source share
4

, , hooks, . , , , -, .py ".py'less .py . , , , normall ( py), /usr/bin/yetanotherfeature, py.

Edit: Nevermind this ( , ), import imp :)

+1

imp module:

daniel@purplehaze:/tmp/test$ cat mymodule
print "woho!"
daniel@purplehaze:/tmp/test$ cat test.py 
import imp
imp.load_source("apanapansson", "mymodule")
daniel@purplehaze:/tmp/test$ python test.py
woho!
daniel@purplehaze:/tmp/test$ 
+9

- setuptools:

"... , script s Windows POSIX. " "script " " -... setuptools , , Windows .exe..."

https://pythonhosted.org/setuptools/setuptools.html#automatic-script-creation

0

Source: https://habr.com/ru/post/1708631/


All Articles