Share a python module between my scripts

lionel.courgnaud
Enthusiast
Enthusiast

Share a python module between my scripts

lionel.courgnaud
Enthusiast
Enthusiast

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.

0 Likes
Reply
Accepted solutions (2)
723 Views
10 Replies
Replies (10)

JeromeBriot
Mentor
Mentor

Hello,

 

You can use sys.path.append() at the begining of the code.

 

0 Likes

lionel.courgnaud
Enthusiast
Enthusiast

Thanks for the answer.

I tried :

import sys

sys.path.append('../mymodulepath')
 
Is there something else to do to have the autocompletion and/or import mymodule.py
It seems I can extend the sys.path but I don't know how to use it.
 
I found a system solution, but I'm not sure it's a good practice :
I created a symlink in my script folder. This symlink is pointing to mymodule.py
in my main script I added  from . import mymodule
now, I can access to mymodule.myfunction() 
 
thés
 
0 Likes

lionel.courgnaud
Enthusiast
Enthusiast
Accepted solution

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.

 

 

 

 

0 Likes

JeromeBriot
Mentor
Mentor

@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

 

0 Likes

JeromeBriot
Mentor
Mentor

You can also directly use relative import:

 

from ..mymodulepath import mymodule

 

0 Likes

lionel.courgnaud
Enthusiast
Enthusiast

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

 

 

0 Likes

JeromeBriot
Mentor
Mentor

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

 

1 Like

lionel.courgnaud
Enthusiast
Enthusiast

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.

0 Likes

JeromeBriot
Mentor
Mentor
Accepted solution

@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:

 

[video]

 

 

 

1 Like

lionel.courgnaud
Enthusiast
Enthusiast

This solution is perfect for me 🙂 Thanks !

0 Likes