Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to lock .py file
#1


Hello All,

Can anyone please help me in finding a way how to lock .py file. By locking i mean that no one shall be able to see the code or modify the code once it's locked but it shall be executable from other applications.

Something like DLLs from .net. we can not see code but it executes.

Thanks
Reply
#2
When you run the code, modules you imported will be extended to pyc files (python compiled). Then you could compile the root file with
>>> import py_compile
>>> py_compile.compile('abc.py')
You can pass these along to clients instead. However there are python uncompilers. Which can reverse this and see the code.

You could build an exe from it with py2exe, pyinstaller, cxfreeze , etc.
However exe's source can be viewable too. Thats how triple AAA games and high end programs get pirated.

Another other option is to obfuscate your code. Or you can write portions in c/c++ and compile it and import it in python.

You can also license it. It will stop tsome folks, but some just wont care.

Python wasnt made to have it code hidden. Its open source and good for open source applications.
https://wiki.python.org/moin/Asking%20fo...%20code%3F
Recommended Tutorials:
Reply
#3
Thanks Metulburr for reply. I already tried Generating *.pyc file and making it bytecoded. My problem is that I am using *.py file in my C# code. As long as i specify file as *.py code works fine but as soon as i define it as *.pyc it gives me this error.
Error:
An exception of type 'System.ArgumentException' occurred in Microsoft.Scripting.dll but was not handled in user code System.ArgumentException was unhandled by user code HResult=-2147024809 Message=File extension '.pyc' is not associated with any language. Source=Microsoft.Scripting StackTrace: at Microsoft.Scripting.Hosting.ScriptRuntime.UseFile(String path) at WpfApp.MainWindow.func() in C:\Users\documents\visual studio 2015\Projects\WpfApp\WpfApp\MainWindow.xaml.cs:line 42 at WpfApp.MainWindow..ctor() in C:\Users\documents\visual studio 2015\Projects\WpfApp\WpfApp\MainWindow.xaml.cs:line 31 InnerException:
My C# code is like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;




namespace WpfApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            func();
        }
        public void func()
        {
                        
            var ipy = Python.CreateRuntime();
            dynamic test = ipy.UseFile("pythonFunctions.pyc");
            lblText.Content=test.calcu(1,2,3,4,5);
            
        }
        
    }
}
It gives me error at line 37. I tried importing dll as reference too but that also didn't work.
Is there any way in C# to convert .pyc back to .py within code itself?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cant find root cause of thread.lock error burlyboys 0 1,541 May-18-2020, 12:51 PM
Last Post: burlyboys
  Read write Lock concerns arvindh 1 2,431 Apr-24-2018, 11:36 PM
Last Post: ODIS
  Brute Force Pad Lock Guesser RedSkeleton007 4 3,915 Mar-03-2018, 07:42 AM
Last Post: RedSkeleton007

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020