I'm trying to migrate to wsgi but I'm getting ImportError exceptions all the time.
I have my modules installed only with .pyc files, I don't have the .py files in the server (I can't change that, sorry). When I add some .py files the ImportError moves away to some other import line.
Is there a way to make WSGI use the .pyc files and work without the .py?
The script with the application entry point is a .py file but it imports some modules which are .pyc
Thanks for your help
You might need to put all the python files on that machine, then convert them to .pyc or .pyo then delete the .py's
I'm suspecting maybe the .pys were generated on a different version of python maybe.
Also paste the import errors .. that might help in diagnoses.
Related
I don't know if my question is ambiguous or not but, I noticed that in Scripts folder inside the python installation folder there are executable files. Each file about a 100kb in size.
FYI: when I open it (or them) using 7Zip I often find a init.py file inside.
Thanks
I have tried researching but can't seem to find the answer.
The setuptools package builder is able to
Automatically generate wrapper scripts or Windows (console and GUI) .exe files for any number of “main” functions in your project. (Note: this is not a py2exe replacement; the .exe files rely on the local Python installation.)
(ref. from setuptools documentation)
Unfortunately the way it is actually done is considered an implementation detail and is not documented.
Stack Overflow has many questions about
How to give someone else a python script protecting the source code
How to compile python files
How to create packages and deploy the code
But I could not find the answer to my problem:
I want to give someone else my python script, without giving him the source code. My current attempt is compiling the file and giving away the .pyc file.
This is for sure not the best solution. Moreover, my code is made by different files. To offer a single executable pyc file, I put the code all together in a single file before compiling it: a true hell for a developer
How can I obtain my goal in a cleaner way?
Side-notes
I know .pyc files are not going to hide so much, but it is for sure better compared to giving .py files
Still, .pyc files can be incredibly problematic (as they can be system-dependant)
You can create a .exe file using pyinstaller.
pip install pyinstaller
then, open terminal in your source code directory and use the command:
pyinstaller --onefile source.py
If you have database connection with python file then it can be added using:
pyinstaller --onefile --add-data 'database.db:.' source.py
Here, :. shows database.db is a source data file and it will copy on the top level of your python application.
using "pyinstaller sroucecode.py --onefile" command will generate an executable file on Windows. This can be a way should it be desired to ship the functionality but hide the code.
There is tool, which might help you to achieve described outcome, but only if destination machine is able to run .exe files.
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 got this error while running the Django app file that I had downloaded from GitHub. How can I solve this problem?
Please go to your home directory. And then:
sudo find . -name "*.pyc" -exec rm -f {} \;
Finally, I found the answer, The project has .pyc files, which holds the previous version information and time date, so this error. After deleting those files I got the output.
You can delete all the .pyc files in your folder to resolve it.
find . -name \*.pyc -delete
Included in your checkout are .pyc files. These are byte cache files, storing cached bytecode so Python can avoid having to parse and compile the source files. Unless you plan to distribute a project without source files, these should not be included.
Just delete all .pyc files located in the same directory as .py files.
The "magic number" in the error message is a version number for the bytecode stored, and specific Python versions only work with specific bytecode magic numbers; the number in your error is equal to 62211 in decimal (when interpreted as a little-endian number), which shows the .pyc files were created with a Python 2.7 interpreter.
Python 3.2 switched to storing .pyc files in separate __pycache__ directories and including the Python version in the filename. However, any .pyc files still located next to the .py files are supported still to allow for bytecode-only releases. It's safe to delete such files because if you were to use a Python 2.7 interpreter in future, then the files will be re-created.
Delete the .pyc files created in your directory .'
ex : i have gitlab.py and gitlab.pyc
later i renamed it into gitlab-api.py
But while running python file , it is using gitlab.pyc so
Traceback (most recent call last):
File "gitlab-api.py", line 1, in
import gitlab
ImportError: bad magic number in 'gitlab': b'\x03\xf3\r\n'
it worked correctly when i deleted the gitlab.pyc
you will need to delete any pyc. pyc is the cache of your app. delete all files that end with .pyc and rerun your app.
If your OS is Windows you have to delete Python's older versions, then you are ready to use pip again. This is the best method, without errors.
Rename the class name or file name that you are importing, this fixed my issue.
I'm running Python 3.4.1 on Windows 7 and thought that after running my .py script in the command line, a directory named _pycache_ would be created in the same directory that my script ran in. It is not there, even after I made sure that 'Show hidden files, folders, and drives' was checked. I looked around here and on Google but can't seem to get an answer that makes this visible.
Can someone help? I'm new to Python and would like to look over the byte code files.
The directory is called __pycache__ (with double underscores).
It'll only be created if Python has permission to create a directory in the same location the .py file lives. The folder is not hidden in any way, if it is not there, then Python did not create it.
Note that .pyc bytecode cache files are only created for modules your code imports; it is not created for the main script file. If you run python.exe foobar.py, no __pycache__/foobar.cpython-34.pyc file is created.