Is it possible to include both .so (cython compiled) and .py files in a folder with same name. Which one will python use if I use import ?
Related
I am new to python. I have a directory of *.py files and subdirectory of *.py files, how can I compile them to catch any syntax error?
Thank you.
To compile all .py file in suppose 'Lib/' directory :
import compileall
compileall.compile_dir('Lib/', force=True)
For more information on the python files compilation, please follow this link. https://docs.python.org/3/library/compileall.html
I have been working on a python project and I am new to it. I have made a small library for my project in which I have several different modules doing different tasks.
For example: I have 5 modules namely add, subtract, multiply, divide and root. I call all these .pyc files into my main.py file and my code runs properly if all of them are in the same folder.
Now, I want to store my main.py at: D:\project\main.py and these 5 .pyc files at : D:\project\Lib\ (In the Lib folder)
I found a solution as to mention the path of the folder Lib into the code but I can not do so as I need to submit the code somewhere and if they try to run this on their PC, it might not import these files.
What would be the possible solution to this?
Inside the main.py file, add the location of your modules to the path variable as below.
os.environ["PATH"] += os.pathsep + your_path
your_path which is relative to your project should be like './Lib'.
Try to import modules after the above step. I guess it should work fine. Let me know.
Note:
Those .pyc files are cache files from executing the .py files. You need to worry about the .py files and not .pyc files.
I made a Python wrapper for an SDK written in C++ using SWIG and built using Microsoft Visual Studio 2010 in Windows 7.
It's been a success, with the module.py and _module.pyd being generated. However, this wrapper heavily depends on a couple .dll files located in the SDK folder outside PythonXY folder.
After I moved the module.py and module.pyd into PythonXY/Lib/site-packages/, and tried running import module, the following error occurs:
ImportError: DLL load failed: The specified module could not be found.
Then, I tried to change the Python working directory to the directory where the .dll files exist, using os.chdir() method from Python module os. This time, running import module output no error. I continued to access the wrapped C++ classes and functions and they worked just fine.
My questions are:
How can I "link" the path containing the .dll files so whenever I want to use module, I don't have to change the working directory?
Is there any way to append additional working directory aside from Python default working directory from within the Python source file? (i.e. not from PATH configuration on Windows)
Is there any way I can link the wrapper with those .dll files dynamically? Meaning that let say the files are copied to another machine and the .dll files are stored in different absolute path directory, the import module would still work?
The point is I want to make this wrapper as portable as possible, across multiple Windows machine.
Thank you.
I found a solution to let module.py find _module.pyd.
Basically, create a new file called module.pth and save it in the same directory as module.py. In this case, the directory is PythonXY/Lib/site-packages/.
Let say you place your _module.pyd inside a directory called _modulelib (PythonXY/Lib/site-packages/_modulelib/), then the content of module.pth should be like this:
_modulelib
I am running pygame (for Python) on Windows. I have some .pyo files and some .pyd files. I have another script for somewhere else that is trying to import one of the .pyd files as a module but I keep getting the error that no such module exists.
Do .pyo files have issues importing .pyd files as modules? What can I do to solve this issue?
It's typically because of one or more of the following:
The .pyd is not in your current path (you said it was in the same folder so that should not be the problem)
A DLL the .pyd depends on is not in your current path. Locate the missing DLL's using depends.exe or its modern rewrite and either copy these dll's to the same folder or add the containing directories to your system path
You're using a debug version of python. Then the module must be renamed from xyz.pyd to xyz_d.pyd.
I'm using cx_Freeze to freeze my python program. On running cx_Freeze, a bunch of PYD files are created, a whole bunch of PYC files are put into a archive named library.zip and a few DLL files are there too.
Could someone tell me the difference between the PYC and the PYD files?
What's the reason for the PYD files not in the library.zip?
Is it possible to put the PYD files into the archive as well?
Thanks.
Disclaimer: I haven't used cx_Freeze in awhile......
.PYD files are DLL machine-code files that contain specific python-required functions.
.PYC files are .py files that have been compiled into bytecode.
so PYDs are machine code and PYCs are bytecode
Now as for why the PYDs aren't in the .zip....I'd imagine it's because those .PYDs are needed by the python interpreter to run the program. What cx_Freeze does is basically this:
compile all .py files and throw the .pyc files in a zip
put all needed .pyd files in the zip
create a stub .py file and put it in the output directory
copy the python.exe and rename to myprogram.exe
copy all .pyd files needed to open the .zip and run the contents
So you're not actually compiling your python file, you're instead renaming the interpeter and freezing all the source files.
I hope this helps.