Installing a python package in a desired folder

2019-07-11 21:19发布

I have downloaded a python package to install, on my ubuntu machine. The package has already a setup.py file to use, but I want to change the default python installation address to something else, for this package specifically (and not for good). So what I tried is:

First in the terminal, I export that address of the new folder: export PYTHONPATH=${PYTHONPATH}:${HOME}/Documents/testfolder/lib/python2.7/site-packages

Then I add this exported address as prefix to the installation command: python setup.py install --prefix=~/Documents/testfolder

The installation goes through. Now to make python always look for this new path as well (next to the default installation path), I export the address in bashrc file: export PYTHONPATH="${PYTHONPATH}:~/Documents/testfolder/lib/python2.7/site-packages"

But now whenever I open a terminal and try to import the installed package, it cannot see ("no module named..."). Only when I open a terminal in the folder where I had the installation files (namely setup.py), and run python, can it then see the package, and it works there.

Why isn't my export in bashrc making the package available from anywhere? Is there something I have done wrong in the above?

1条回答
狗以群分
2楼-- · 2019-07-11 21:46

To answer your question about the export path. Do you have $PYTHONPATH as a part of your $PATH? If not you should add it to path.

The best way to handle this scenario in my opinion is to use a virtual python environment. There are a couple to choose from, but I like virtualenv the best. The reason to take this approach is because you can manage different versions of python in separate folders. And have separate packages installed in these folders. I recommend looking into it as it is a very useful tool. If you want an examole of how to use it i can provide that https://virtualenv.pypa.io/en/stable/

查看更多
登录 后发表回答