Adding an Installed PIP Package to an Automatic Path

For my foo package, I use the following setup.py:

from setuptools import setup setup(name='foo', version='0.0.1', description='Lol', url='https://github.com/foo/foo', author='legend', author_email=' lol@gmail.com ', license='GPLv3', packages=['foo'], install_requires=["bar"], entry_points = {'console_scripts': ['foo = foo:main']}, keywords = ['foo'], zip_safe=False) 

When testing on my Arch system, it automatically added a script to PATH so that I could just run foo on my command line and run the main () function automatically. Then I booted up the virtual machine and tested it on Windows 7. Pip installed the package just fine, but it was not in my way!

reference

+5
source share
2 answers

setuptools , pip and easy_install do not change the system PATH variable. The <python directory>\Scripts , where they all install the default script, is usually added to the PATH by the Python installer during installation. You can use the <python directory>\Tools\scripts\win_add2path.py script to fix this if for some reason this does not happen.

The above setup.py file example worked fine for me (using the Scripts directory in PATH), by the way. I tested it with

 python setup.py bdist_wheel pip install dist\foo-0.0.1-py3-none-any.whl 

and

 python setup.py sdist pip install dist\foo-0.0.1.zip 
+2
source

Do not expect pip or easy_install change your PATH , their task is to install the package in the current environment.

On Linux, if you use the global Python environment, you will most likely need root privileges, so you usually do:

 $ sudo pip install <package> 

However, this method is not recommended because it spoils the system-wide Python environment (imagine that two applications have slightly different requirements for the same package version and you may have a problem).

The recommended method is to use some kind of virtualenv, which allows you to install the python package in a separate python environment, which is also easily removed and recreated.

How do I install python based scripts on a system

It looks like you have a custom python based script that you want to use on your system.

For this scenario, I use the following method (assuming the virtualenv tool is installed in system-wide python):

 $ mkdir ~/apps $ mkdir ~/apps/myutil $ cd ~/apps/myutil $ virtualenv .env $ source .env/bin/activate (.env)$ pip install <package-or-more> 

Now you have all the script installed by pip installed in the ~/apps/myutil/.env/bin , let's call it myscript (there can be more of them).

The rest of the step is to create a symbolic link from some directory that is already on PATH , for example. in /usr/local/bin :

 $ cd /usr/local/bin $ sudo ln -s ~/apps/myutil/.env/bin/myscript 

From now on, you can invoke the myscript command even without activating virtualenv.

Script update

If you need to install a later version of the script:

 $ cd ~/apps/myutil $ source .env/bin/activate (.env)$ pip install --upgrade <package-or-more> 

Since you have a related script, it will be automatically available in the latest version.

Naming with virtualenvwrapper

virtualenvwrapper allows you to create multiple named virtual servers and provide easy activation and deactivation. In this case, I do the following:

 $ mkvirtualenv bin-myscript (bin-myscript)$ pip install <package-or-more> (bin-myscript)$ which `myscript` ~/.Evns/bin-myscript/bin/myscript (bin-myscript)$ cd /usr/local/bin (bin-myscript)$ sudo ln -s ~/.Evns/bin-myscript/bin/myscript 

Updating is even easier:

 $ workon bin-myscript (bin-myscript)$ pip install --upgrade <package-or-two> 

and you are done

alternative to toxin

tox is a great tool for automatically creating virtual virtual machines and testing. I use it to create virtualenvs in directories that I like. See my other SO answer for more information.

+3
source

Source: https://habr.com/ru/post/1245341/


All Articles