pyc & importing modules

7229
8
09-03-2016 03:30 AM
MatthiasBuehler
Deactivated User

Hi,

I'd like to create .pyc files and create scripts that will still run only using .pyc files (for sharing the files).

important is that I would also like to use import modules I have created, so it should work to import pyc files or compile all files into one main pyc file.

can I get a short into in how to create those files?

please let me know.. thanks!

matt

0 Kudos
8 Replies
DanPatterson_Retired
MVP Emeritus

if you need to try, 32.10. py_compile — Compile Python source files — Python 2.7.12 documentation 

There are many references to this, just search .... create pyc python ... as a search phrase,  e.g.

python - How can I manually generate a .pyc file from a .py file - Stack Overflow 

import py_compile
py_compile.compile("file.py")
MatthiasBuehler
Deactivated User

Ah nice.. so there's no specific steps in ArcPy, all standard Python stuff, right?

0 Kudos
DanPatterson_Retired
MVP Emeritus

give it whirl with your code, I don't use pyc... the only advantage is for use in a situation where people are working in an environment that would restrict the compilation on their machine.  If it for hiding/protecting your code, it will slow down most, but not guarantee it... for that produce a license agreement

MatthiasBuehler
Deactivated User

Ok, thanks for the inputs, as always!

I'll try!

Matt

0 Kudos
DanPatterson_Retired
MVP Emeritus

Report? did it work?

0 Kudos
MatthiasBuehler
Deactivated User

Hi,

Sorry, I've been out of town for work for 2 weeks .. and this week's been busy too.

But thanks for checking back.

The compiling part did work, yes. Though what I ended up using are the automatically generated .pyc files in the same folder. I am not sure why I did not see them being produced in the first place, this is puzzling. It may be a folder refresh issue.

So basically, the structure I'm using now is 1 py file and 1 pyc file, where in the first file I just call a method in the second file, passing on the relevant parameters / user settings.

Thanks !!

matt

0 Kudos
DanPatterson_Retired
MVP Emeritus

pyc files are created when a file/module is created.  It remains the same unless edits to the underlying script are made, just be aware that if you edit your script from which the pyc was created, that you need to use the new pyc

Luke_Pinner
MVP Regular Contributor