Changing Python path variable in Openshift NodeJS cartridge - python

I have a NodeJS web server (Express) locally that uses Python-Shell to fire off Python scripts (mostly scraper stuff using PyCurl). The Python scripts also use various modules that I have installed using pip. This all works locally with no issues, running Node v4.1.1 and Python 2.7.10.
Now, I need a server to run this setup on, and I'm cheap, so I really want to use Openshift, which I have used before for other Node-only projects.
My problem is that I need to work with Python, and while it is installed (v2.6 anyway), I can't do much with it. Pip is not installed at all, and I don't have administrative rights to install it. Easy_install seems to be present, but I get permission issues when trying to execute it (even just the test via "easy_install" with no parameters). I need pip to install several modules for my Python scripts.
I was able to follow the instructions here and get Python 2.7, setuptools, and pip all installed successfully, but they are only available by prefixing the path $OPENSHIFT_DATA_DIR/bin (which resolves to /var/lib/openshift/[my-id]/app-root/data/bin). So...
\> $OPENSHIFT_DATA_DIR/bin/python -V
Python 2.7.3
I can also successfully run my Python scripts if I prefix the python command with that path.
So my question is how can I either get python-shell to use that path for Python, or how can I update Openshift's environment variables so that the standard "python" command is pointed to my manually-built v2.7.3 executable?
I'm stumped, but I know next to nothing about Linux or Openshift's architecture specifically, so I'm hoping you guru's out there can help me out!

If someone ever has a real answer to this, feel free to post it up, but I ended up just abandoning Python altogether and re-wrote the scraper in Node using Cheerio. Much better!

Related

Running pip3 on MacOS Sierra (Python newbie) - do I need a virtualenv?

I'm running MacOS Sierra 10.12.6
By default the system came with Python 2.7.10
I installed Python 3.6.3 (with IDLE) so I can learn Python (3). I understand that this is normal as MacOS may rely on Python 2.x for some programs. Either way, Python3 runs just fine if I run python3 from the command line/terminal, or if I use IDLE (which defaults to Python 3).
Now I want to install some libraries like Beautiful Soup.
And I believe I can install it as follows:
pip3 install beautifulsoup4
which should automatically install it. However, I read that it's recommended to use virtualenv on Mac BEFORE I run the above command. As a newbie, I don't want to mess anything up on my PC, so can anyone point me out how I can do this correctly?
For example, I can follow this link: http://sourabhbajaj.com/mac-setup/Python/virtualenv.html
But I just want to write here to make sure I'm following the right article/commands before I do it. Just being super careful!
Also, can I make a folder with my "virtual environment" and then add sub-folders inside that for each project? Meaning, I don't need to do this everytime, I have one virtual environment and any project that I do just is a subfolder within that space so I can use any libraries that I installed. Just trying to grasp the concept.
Thanks!
Sorry to add confusion.. this can be a tough subject for someone starting out.
The official docs recommend venv, which is similar to, but slightly different than virtualenv.
I would strongly recommend pycharm. It will create your venv for you as part of your project, which you might find helpful.
[Edit: Some other virtual environment features of pycharm that will help you].
If you type in an import statement for a package that isn't installed, it will offer to install it for you.
typing alt-F12 will bring you up a console with your virtual environment active
It syncs up your requirements.txt document for you
It manages your virtual environment path for you (as long as you are running inside pycharm), helping avoid import problems that many newcomers have with virtual environments.
I am not affiliated with pycharm, btw -- I just think it is a great tool for python developers, especially for newcomers, and its treatment of virtual environments is especially helpful.
You create one virtualenv for each project as a way of keeping track of the specific dependencies to keep them minimal which then makes it easier when you want to share projects with other people.
But this is not something you need. No harm comes from installing packages in your real environment as well. So you can safely run
pip3 install beautifulsoup4

Installing and using python on a client computer

I am writing a small python installer application that will run on linux. However, i don't know which Linux in advanced, so i cant write for any specific python version since i have no idea what is installed on the client computer, if any (do i? is there any thumb rule for python version preinstalled on a Linux machine?).
Is there a optimal way to install a local python version on a client's computer in some temporary directory, use it for the application run-time, than remove it when it is done?
Do you know the machines architecture? If so simply download the python version you want to install on the linux architecture on your server/box/your computer then compress it and upload it to a server somewhere. (or use the link on python.org)
then simply make a bash script to download it and uncompress it. Then install it using shell commands like make or cmake ./configure within the install directory
If you dont know the architecture or are trying to use it on some generic or other you could cross compile it using these cross compilers to cross compile python (or anything coded in c(/c++?))
http://pastebin.com/mxWqBvy8
Typically the general 'rule of thumb' for the python version would be python2.7.x
You could use Pyinstaller to "compile" your python (its origanally win) there is a linux version somewhere, fairly sure its as easy as pyinstaller test.py)
Removing it shouldnt be that hard, rm is universal everywhere
You can bundle your programs with Python itself without requiring the target machine installed Python Interpreter on it.
Check this out: https://wiki.python.org/moin/Freeze

App built with non-system Python using py2app in pyenv not runnable on other machines

My understanding was that as long as a non-Apple-default Python is employed to build, that the end-user need not install Python him/herself to execute a py2app-built app. In developing and testing the app in my own environment, I obviously have Python installed. Specifically, I built in a pyenv with with a python.org install, not Apple's own. Yet when I give the app to an end-user who doesn't have Python installed, she gets:
A Python runtime could not be located. You may need to install a
framework build of Python, or edit the PyRuntimeLocations array in
this application's Info.plist file.
The second line is concerning; if what it states is true, then a separate app instance would need to be built for every possible location of an end-user's install e.g /usr/bin, /Library/Frameworks etc.
UPDATE: Info.plist defines:
<key>PythonExecutable</key>
<string>/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python</string
Yet the end-user in question only has a system install in /usr/bin.
Does this mean that every end-user needs to have an externally-installed Python, and it must live in /Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python
What if they don't have a non-Apple Python? What if they have a non-Apple Python but it's not 2.6? How can this somewhat hardcoded dependency be avoided?
py2app automatically defaults to --semi-standalone mode if it thinks you are using the system interpreter. Your interpreter from Python.org shouldn't count as a "system" interpreter, but you could see what py2app thinks using this command:
$ python -c "import py2app.build_app; print py2app.build_app.is_system()"
False
One issue to watch out for: After I installed a Python.org interpreter today, bash didn't update it's hash cache, causing strange incompatibilities when I launched python. I had to type hash -r python to reset the cache and make sure the correct version of python was getting used. (Another way to fix this is to log out and log in again.) I suppose it's possible that the same issue could have caused py2app to be confused about whether or not you were using the system python.
If that doesn't do the trick, then try installing your python interpreter to a weird location, like ~/mypython or something like that, just to make sure there's no way it can be confused for a system python.
As a last resort, I suppose you could just hack the py2app source code so that is_system() always returns False. Not sure if that would have any adverse consequences, though.
PS -- Here's a little tutorial on using py2app with a conda-packaged application:
https://github.com/stuarteberg/helloworld Not exactly relevant to your problem here, but you could compare it with your own setup and look for any conspicuous differences.

hostgator - change python version

I opened a ticket with hostgator but trying to fix this on my own.
On my shared hostgator account, when I do python -V. It gives me 2.6.6
I need to use 2.7 which they do have, but doesn't work on my shebang line.
1)So, how do I change my python command to point to 2.7
I tried modifying the .bash_profile but the 2.7 path is added at the end.
2)Actually my shebang line needs to work as well.
I found the below comment for the same problem posted by someone else on Google Groups. I am also experiencing the same problem, but haven't yet tried the below mentioned approach yet. Please try if you find it might help.
Google Groups link
Hi, I'm also using hostgator to host a django project.
first of all note that they have different plans with different features, what I'm going to write applies to their shared hosting plans.
Looks like they are using some version of centos, which comes with python 2.6 I believe.
Installing a python from source on your home folder is a good idea, but unfortunately hostgator blocks access to gcc.
on the other hand they already have python 2.7.3 installed, juts use python2.7 as the executable, try running "python2.7 -V"
btw, they also have python2.6 for 2.6.6 and python3 for 3.2.3
after that you should use virtualenv to create a project specific env so you can install things using pip. download the virtualenv.py file to you project folder and run it from there. for the index.fcgi file you should put this in the first line:
#!/home/username/projectname/env/bin/python
other than that I think it should be standard

Deploying a python CGI app

I have developed a python CGI application which works just fine on my development box. My hosting provider however gives me little control of its server: I use a lot of custom stuff in my python environment (like sqlalchemy and mako templating) and the servers python version is far too old to be used. My question is: how do I set up a isolated, complete, standalone python environment in my home directory and install my required modules to run my app? ...the easiest way ;)
how do I set up a isolated, complete, standalone python environment in my home directory
mkdir /home/me/.local (if it doesn't already exist. You don't have to use .local but it is becoming the normal place to put this)
mkdir /home/me/.local/src (ditto)
cd /home/me/.local/src
wget http://python.org/ftp/python/2.6.4/Python-2.6.4.tgz
gzip -d Python-2.6.4.tgz
tar xf Python-2.6.4.tar
cd Python-2.6.4
./configure --prefix=/home/me/.local
make
make install
Hopefully you can now run Python:
/home/me/.local/bin/python
Install packages you need using the usual setup.py script, but with your version of Python:
/home/me/.local/bin/python setup.py install
Set hashbang on CGI files to use your version of Python:
#!/home/me/.local/bin/python
Consider migrating your application to WSGI if you can. You can of course still deploy WSGI apps through CGI using a wsgiref.handlers.CGIHandler for now, but in the future when you have a less woeful hosting environment you'll be able to deploy using a much less wasteful server interface such as mod_wsgi.
In your shoes, I'd use pyinstaller to bundle Python, my code, and all my dependencies into one installer executable, upload it, and run it. Just be sure to use the SVN trunk of pyinstaller -- the "released" version is WAY obsolete.
Be aware that with SQLAlchemy and everything else, with CGI you may find out you're really slow, since you're paying the full startup price everytime the page gets visited. But if CGI is all you can afford, I guess that's the way I would try to cope!-)
This looks like a job for virtualenv. From the site:
Also, what if you can't install packages into the global site-packages directory? For instance, on a shared host.
This looks to be right up your alley.
I am on Dreamhost's shared plan. Besides CGI, they also offer FastCGI which makes things much faster than CGI. You should check if your hosting provider offers that. Or maybe they provide Passenger for Ruby that you could piggyback your Python with.
If you compile Python yourself, keep in mind the UCS setting if you try to install precompiled packages and experience failures. See the StackOverflow article. Dreamhost's wiki has some advice on how you could build and deploy Python yourself on their servers; you might want to adapt to your needs.

Categories