简体   繁体   中英

Installing a library in Python

I am new to MAC OS, and I need to install a library in Python called btmorph . In order to install it, I have to write these commands in the terminal:

git clone https://bitbucket.org/btorb/btmorph.git
cd btmorph
export PYTHONPATH=$(pwd):$PYTHONPATH

and then they said:

The above commands will temporarily set your $PYTHONPATH. Add the appropriate path in your .bashrc to make add the package permanently.

the first commands were executed successfully, but the last one is asking for PYTHONPATH which I don't know, and I am not sure if I want to change it permanently!

and then to test it I have to write:

nosetests -v --nocapture tests/structs_test.py
nosetests -v --nocapture tests/stats_test.py

I am sorry but I am a beginner in MAC. Thank you very much.

To install a python library that does not have a setup.py file, the location of the library's root directory needs to be appended to the $PYTHONPATH environment variable for Python to be able to locate it. This is what the third command export PYTHONPATH=$(pwd):$PYTHONPATH does on a temporary basis.

To do it more permanently, that line, or more specifically, a similar one, needs to find its way into one of the files that the bash shell loads every time a new Terminal window is opened. ~/.bashrc is one of these files but ~/.bash_profile is another, and one that is arguably the better choice for a simple install on Mac OS X .

For the btmorph example specifically, there is a one-liner that can get the job done for you. I've tested it myself here, and so long as you have all of btmorph's dependencies installed , python should load the library with no problem.

If you have already executed the first two commands you listed, you should already be inside the directory into which you cloned the btmorph source code. On a default Terminal session, your prompt should read something like Maestros-Mac:btmorph TheMaestro$ . If it does, you're ready to go. (You can also use the pwd or p rint w orking d irectory command to see the full path to your current directory)

Copying the following command and pasting it in your Terminal window will write the correct line to your .bash_profile file (or create the file and write the line to it if it doesn't already exist) and then load that file.

echo \\export PYTHONPATH=$PYTHONPATH:$PWD>>~/.bash_profile && source ~/.bash_profile

Once you have run this command, you should be able to import btmorph from within a Python interpreter, and the change should persist over time.

Keep in mind that since the location of the btmorph folder where you created it has been hardcoded into the $PYTHONPATH variable (that's what the $PWD part of the command does), you cannot move the btmorph folder from where it is now, or Python won't be able to find it anymore. If you want to store the folder somewhere else, I would cd to that folder and git clone it in there to start with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM