简体   繁体   中英

Pycharm: How to build and install (on other computer) custom module?

In pycharm (2018.3.5 community edition) I have created a new project my_module. In my_module dir, I have created a new (python) file mymodule.py in which I have put:

LOG_LEVEL = 1

def print_text(text):
    print(text)

class MyClass:
    def __init__(self, text):
        self.text = text

    def print(self):
        print(self.text)


if __name__ == '__main__':
    print('LOG_LEVEL =', LOG_LEVEL)
    print_text('abc')
    obj = MyClass('test')
    obj.print()

If run mymodule.py I get output:

LOG_LEVEL = 1
abc
test

which is OK.

I would like to use this mymodule.py privately on several computers by simple import mymodule (from any location). I found out that I can use pycharm's menu Tools > Create setup.py to create setup.py with the contents:

from setuptools import setup

setup(
    name='mymodule',
    version='0.0.1',
    packages=[''],
    url='',
    license='',
    author='myself',
    author_email='',
    description=''
)

If I enter (cwd) my_module directory (in which is mymodule.py) and execute:

sudo pip3 install .

Processing /home/user/Python/my_module
Installing collected packages: mymodule
  Running setup.py install for mymodule ... done
Successfully installed mymodule-0.0.1

After this action I can start python interpreter out of pycharm in any folder and execute:

Python 3.7.2 (default, Jan 10 2019, 23:51:51) 
[GCC 8.2.1 20181127] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mymodule
>>> mymodule.LOG_LEVEL
1
>>> mymodule.print_text('abc')
abc
>>> obj = mymodule.MyClass('test')
>>> obj.print()
test

Which is OK. I read that instead of sudo pip3 install . I can use sudo install -e . which will update mymodule in python repository with each change of mymodule.py (haven't tried it yet, but I would rather update mymodule from version to version manually).

For development computer so far is so good. But if I want to install mymodule on some other computer, what should I do? I believe that I should build a tar.gz from my_module directory with all necessary files, transfer this tar.gz file to other computer and than execute there... what?

mymodule should stay private, I don't want to expose it publically (I am still in the learning process).

I noticed that pycharm has menu option Tools > Run setup.py Task, that provides (not all options are visible) something like this:

在此输入图像描述

Can someone give to me a hint how to:

  1. update module on development computer? Should I just increment the version in setup.py and afterwards execute sudo pip3 install . if I would like to manually control when new version is released locally?
    Is there a better way (less steps) to do it directly from pycharm somehow?

  2. What would be the easiest way (I would like to use a script to copy and install mymodule on some other computers) to install/update mymodule on other computers (privately)?

If you don't want to publish your module on pypi then the simplest version of private publishing is to put your code in github and declare it as a VCS dependency in other projects. See this for more details.

Or even simpler just checkout a git tag that versions your library and pip install the local checked out code.

By the way pycharm has nothing to do with this. I'd recommend you also get used to the command line python tools.

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