Hello,
I'd like to share a homemade module (containing only functions) between all my scripts.
My script Fusion Folder looks like :
Scripts
|_ myscript1
|_ myscript1.py
|_ myscript2
|_ myscript2.py
It seems I can't store my custom mymodule.py @ the root of fusion scripts folder ("Scripts")
How could I do to import this module into each of my scripts without copying it into each folders ?
I looked for some paths where I could copy mymodule.py but I found nothing.
thanks for your precious help.
Solved! Go to Solution.
Solved by JeromeBriot. Go to Solution.
Solved by lionel.courgnaud. Go to Solution.
Hello,
You can use sys.path.append() at the begining of the code.
Jérôme Briot, Freelance engineer - Mechanical design and prototyping
3D Print Plus / Pro | IDF Import | GitHub To Fusion 360 | Tube Bending Data Exchanger | Slice Data Export
Memory Used | Basic Calculator | Check Computer Specifications | Import spline from any CSV file
Thanks for the answer.
I tried :
import sys
One solution I found is to create a symlink for the module (but it's not a real good python practice)
Exemple :
Scripts
|_ myscript1
|_ myscript1.py
|_ myscript2
|_ myscript2.py
|_sharedmodule
|_sharedmodule.py
Symlink in myscript1/ => ln -s ./sharedmodule/sharedmodule.py sharedmodule.py
Add to file myscript1.py => from . import sharedmodule
Call in this file sharedmodule.myfunction()
I noticed I had to restart visual studio code from Fusion360 to make it works.
@lionel.courgnaud wrote:
import sys
sys.path.append('../mymodulepath')Is there something else to do to have the autocompletion and/or import mymodule.py ?
Try this:
import sys
sys.path.append('../mymodulepath')
import mymodule
Jérôme Briot, Freelance engineer - Mechanical design and prototyping
3D Print Plus / Pro | IDF Import | GitHub To Fusion 360 | Tube Bending Data Exchanger | Slice Data Export
Memory Used | Basic Calculator | Check Computer Specifications | Import spline from any CSV file
You can also directly use relative import:
from ..mymodulepath import mymodule
Jérôme Briot, Freelance engineer - Mechanical design and prototyping
3D Print Plus / Pro | IDF Import | GitHub To Fusion 360 | Tube Bending Data Exchanger | Slice Data Export
Memory Used | Basic Calculator | Check Computer Specifications | Import spline from any CSV file
hello.
thanks for your help. I tried to import mymodule but the only result I had was
ImportError: attempted relative import beyond top-level package
Another attempt:
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from mymodulepath import mymodule
Or:
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'mymodulepath'))
import mymodule
Jérôme Briot, Freelance engineer - Mechanical design and prototyping
3D Print Plus / Pro | IDF Import | GitHub To Fusion 360 | Tube Bending Data Exchanger | Slice Data Export
Memory Used | Basic Calculator | Check Computer Specifications | Import spline from any CSV file
Hello,
I tried with this 2 declarations, and I can run the script, but I have no autocompletion writing my scripts.
A symlink is the best (and ugly) way I found to share module.
@lionel.courgnaud wrote:
I have no autocompletion writing my scripts.
Open the settings.json file in the .vscode folder and add the following entry as an item in the "python.analysis.extraPaths" list:
"${workspaceFolder}/../modules"
Watch this video:
Jérôme Briot, Freelance engineer - Mechanical design and prototyping
3D Print Plus / Pro | IDF Import | GitHub To Fusion 360 | Tube Bending Data Exchanger | Slice Data Export
Memory Used | Basic Calculator | Check Computer Specifications | Import spline from any CSV file
Can't find what you're looking for? Ask the community or share your knowledge.