0

I have a python script which will be executed by the system python (on OSX /usr/bin/python). This script uses an external package (in my case PyYaml).

How can I provide the package without pip-installing it into the system python. I do not want to create a virtual environment for this script either, because the script is meant to run on other "vanilla" OSX machines as well.

I'm thinking of creating a directory under the same path where the script resides and "dumping" the external package there. But in what form? And what do I have to do in my script, so that it "sees" the locally dumped package? Just amend sys.path?

4
  • Sometimes I've been successful just downloading the "source package" (.tar.gz, .zip, etc) (in your case I think it's PyYaml) into any directory then append that directory to the $PYTHONPATH environment variable (OS dependent). On Mac/Linux I just set export PYTHONPATH=$PYTHONPATH:<path_to_python_packages_directory> , re-load the environment. Then "import the_package" in python. Unfortunately, for some packages that depend on CPython, or "C" source files that go to the OS level, it's not possible, or rarely works, but does for most python packages. Commented Mar 7, 2017 at 0:36
  • I tried it with pyYaml and it seems like it worked (I could import it), but there are some "C" source files in that package, so it may not work as expected. Commented Mar 7, 2017 at 0:38
  • @downshift Thanks for this. It should be possible to use just PyYaml's py-files, because PyYaml contains according to the doco "both pure-Python and fast LibYAML-based parsers and emitters" (pyyaml.org/wiki/PyYAML). So aslong as I do not import CLoader and CDumper from yaml I should be fine. Commented Mar 7, 2017 at 1:15
  • ok cool, sounds good Commented Mar 7, 2017 at 1:16

2 Answers 2

1

You can just copypaste the pyYaml's *.py files into your script directory and import them like you would import your own modules. This is not the most beautiful solution, but it works.
If you intend to use C backend, it'd be more difficult. You either need to make sure your installer compiles them correctly, or make platform-dependent distributions with the compiled C modules. But those are there for speed and pure Python code can be enough.

Sign up to request clarification or add additional context in comments.

Comments

0

As an enhancement to Synedraacus's answer: (*)

You can copy the whole yaml subdirectory of PyYAML into your script directory (which is in the PyYAML zip file lib/yaml if you use Python 2 and lib3/yaml for Python 3). Then you can use it in your code as if you had PyYaml pip-installed. Just write:

import yaml

This works of course only if you don't want to use the C backend.


(*) I tried to add this detail to Synedraacus's answer, but my edit got rejected. Therefore I add this enhancement as a separate answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.