PyDev package build and compile from command line in linux - python

I have created a PyDev project in eclipse. I have added PyDev packages to the project. How can I build and compile PyDev package from command line. I am using linux virtual enviornment. The project structure is shown below.
e.g. I want to build and compile "bridge" package from command line. (reference project structure in image). This package do not have dependencies. So that it could be build and compiled individually.
PyDev Project structure

You don't really compile files for Python, just run them and their bytecode will be generated on demand and then used to run the code.
See: http://www.pydev.org/manual_101_run.html for details on how to run your code inside PyDev.

Related

Python virtualenv activation working but interpreter doesn't

I've just setup a new environment for my project and uploaded a python repository including bin, lib and project folder. I'm pretty sure I did same previously and it worked without problem. Now when doing the same on an AWS environment I get the error
-bash: /projects/scrapy/bin/python2.7: cannot execute binary file. However when doing source /projects/scrapy/bin/activate it successfully activates the environment.
From what I understand, python should be able to execute without any issue no matter the environment ?
Any help or pointing to the right direction would be much appreciated!
python should be able to execute without any issue no matter the environment ?
No, the Python binary is tied to your specific OS and computer architecture. Python source code can usually be run on different machines (provided you didn't use OS-specific features), but that's only made possible by compiling a Python interpreter for the specific target environment first.
In other words, a Python binary compiled to run on macOS will not work on Linux.
All that source bin/activate achieves is that it configures your terminal setting to use the bin directory as the first directory on the PATH search path. This doesn't make bin/python work in another environment, it just means that both environments have a working shell interpreter that can run that script.
Create a new virtualenv with a Python binary compiled for Linux, and install the same packages there. Use Pipenv or a requirements.txt file to transfer the dependencies from Mac to Linux.
For example, using Pipenv you'd copy over the Pipfile and Pipfile.lock files to the other computer, then run pipenv install in the directory there and re-create the virtualenv and dependencies from those files.
I recommend you read up on Python development best practices in the The Hitchhiker’s Guide to Python; this includes such topics on how to manage an environment for a project.

How to run python production on customer environment

I have some python application that should run on customer site. I compile my py files to pyc (python byte code).
What is the standard way to run the app on the customer environment? The options I see are:
As part of my installer, install some python distribution, i.e Anaconda.
Require the customer to have python installed in their environment.
Bring python libraries and executable along with my code and run it directly from my installation dir.
Convert the scripts to exe using some py-to-exe tool.
Application usage: The app is used as a tool to calculate statistics for my main product. The customer won't run it explicitly. It won't have any GUI.
Customer environment will be x64 Windows machine. No other restrictions.
Any recommendations or comments? I couldn't find such discussions on the web.
Given your requirements, the last two options seem most viable:
Bring python libraries and executable along with my code and run it directly from my installation dir.
Convert the scripts to exe using some py-to-exe tool.
You can either package your code, or freeze your code and create an executable for the target OS.
Given your requirements, I'd suggest the latter. You can go through the Python Packaging Guide for more details regarding packaging.

Using py2exe in a virtualenv

I have a Python script I developed within a virtualenv on Windows (Python 2.7).
I would now like to compile it into a single EXE using Py2exe.
I've read and read the docs and stackoverflow, and yet I can't find a simple answer: How do I do this? I tried just installing py2exe (via the downloadable installer), but of course that doesn't work because it uses the system-level python, which doesn't have the dependencies for my script installed. It needs to use the virtualenv - but there doesn't seem to be such an option.
I did manage to get bbfreeze to work, but it outputs a dist folder crammed with files, and I just want a simple EXE file (one file) for my simple script, and I understand Py2Exe can do this.
tl;dr: How do I run Py2Exe within the context of a virtualenv so it correctly imports dependencies?
You can do that this way:
Activate your virtualenv and then ...
easy_install py2exe-0.6.9.win32-py2.7.exe
Installing py2exe into your virtual env should be straightforward. You'll need Visual Studio 2008, the express version should work. Launch a 2008 Command Prompt and Activate your virtual env. Change into the directory that contains the py2exe source and run python setup.py install. You can verify that py2exe is in the correct environment by attempting to import it from an interactive shell. I tested myself earlier today (had to install virtualenv). It works exactly as expected.

Is there a way to produce executables from python scripts using makefiles?

I have a python script "MAlice.py" (main) which depends upon several other scripts including yacc.py and lex.py and whatever they in turn import.
I don't have a lot of experience writing makefiles and I'm wondering how I can produce an executable called "compile" using this code, so that someone could call:
make
./compile "someTestFile.alice"
I'm working in Ubuntu.
I think what you are looking for is a way to package your python project into a self-contained executable. It is not a compiling process but rather just a bundling into a portable environment. This will include your python interpreter.
You can look into pyinstaller for linux or windows. And py2app for osx (pyinstaller would work on osx as well to create just a single file)
If what you are after is the ability to provide a source package to someone and for them to be able to run a build command, and have an executable entry poiny called "compile", then what you want is simply a setuptools script. This will install all the declared dependencies and will create any named entry points you define.
http://packages.python.org/an_example_pypi_project/setuptools.html
Python scripts aren't compiled; they're interpreted. You can turn MAlice.py itself into an executable script by adding this as the very first line:
#!/usr/bin/env python
And making the script executable:
chmod a+x MAlice.py
So then you can simply run it like this:
./MAlice.py "someTestFile.alice"

How to setup springpython with Jython and Eclipse/PyDev?

I'm having trouble setting up SpringPython with PyDev and Jython
I've installed Spring python by:
jython setup.py install
and the setup installed the library to my jython installation successfully.
See!:
In my PyDev project i've selected the jython interpreter and have c:\jython2.5.1\Lib\site-packages in my Library paths:
My eclipse environment fails to resolve the new classes:
What else do I need to do to install this baby?
Perhaps I should just be using the Java version of spring...
Thanks SO!
The python interpreter that is used to compile your Python files is specified by PyDev on the project level. I suspect that while you do have Jython installed, your Eclipse project (katas) still uses CPython.
Perform the following steps to fix this:
Open your project properties: right-click your project folder ("katas") and select properties
Select tab "PyDev - Interpreter/Grammar"
Switch Python to "Jython"
Your files should now be compiled correctly using Jython.
As noted by the asker himself, you may need to restart your editor (or the Eclipse itself).
Turns out that I just needed to restart eclipse, there you go.

Categories