Jump to content

Importing into Python


Recommended Posts

I have read many of the forum posts on importing, however I think I'm more confused that I was.

 

I have a bunch of standalone functions that I use in multiple scripts. Nothing fancy, just basic utility functions.  I have taken them all and placed them into a separate python file and I would like to import these functions into my scripts.

 

I thought it would be as simple as making sure the path is in the script path and then doing in import.

 

My functions are in a file called Utilities.py and I'm doing a 

import Utilities

 

When the code runs, I get a No Module Name.

 

Is there a way to include the path into what is being imported, that seems like it would be cleanest option.

 

Thanks

 

Link to comment

@Martin Crawford ,

   Open the Script Editor window. At the bottom there is a button "Script Options...". Click it and you will see a path to your Marionette folder in your user folder. ADD a new path to your utility functions folder. You can have more than one .py file in that folder. Then use the import command at the top of your script to include your functions file. 

e.g., import MyPyFunctions

        where MyPyFunctions.py is your utility function file.

 

   If this isn't clear enough, someone more versed in VW Python will explain better.

 

HTH,

Raymond

Link to comment

I think I was pointing to the wrong folder, so I can not import the module, however inside the Utility.py module, I am getting an error on all the vs. commands. I tried putting a import vs at the top of the Utility.py however this does not work.

 

What do I do to get the vs command usable in the Utility.py script.

 

Thanks

 

Link to comment

@Martin Crawford ,

   I'm not sure exactly what you did. I assume you have a MAIN.py file (I'm guessing at the name) and a Utility.py file. The import command should go at the top of the MAIN.py file, which is the file you will execute. This will enable all of the functions defined in the Utility.py file to be called from the MAIN program.

 

import Utility   # goes at top of MAIN.py

 

   If I guessed wrong about how you set it up, please explain in greater detail.

 

Thanks,

Raymond

  • Like 1
Link to comment

I think my core issue is the my Utility.ps was being cached and any changes I made were not being picked up.

 

I'm not sure if this is the best way, however for some of my functions used in the Utility.py I am importing what I need inside the function.

 

For example, to get the ConvertToFeet function to see the Fraction command, I did the following:

 

#*****************************************************

# Convert to Feet & Inches & Fractions

#*****************************************************

def ConvertToFeet(decimalInch):

    from fractions import Fraction

 

    feet = (int((decimalInch / 12)))

    inch1 = ((decimalInch / 12) - feet)

    inch2 = (inch1 * 12)

    inch3 = (int(inch2))

    frac1 = inch2 - inch3

    frac2 = int(round(frac1 * 16,0)) / 16

    frac3 = Fraction(frac2).limit_denominator(16)

    if frac2 == 0:

        feetString = str(feet) + "' " + str(inch3) + '"'

    else:

        feetString = str(feet) + "' " + str(inch3) + " " + str(frac3) + '"'

 

return(feetString)

 

I will post a different topic on how the cache works and how to work around not having to start VW every time I make a change.

 

Thanks

 

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...