How to include a text file in a python installed package?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I have created a python package that looks like this:



/command
/command
module.py
__main__.py
README.md
setup.py
file.txt


To install i run:



sudo python setup.py install


Now when i call



$ command


it shows this error:



FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'


There are approximately the contents of modules setup.py, __main__.py and module.py



setup.py



import setuptools

setuptools.setup(
name='command',
...
entry_points={
'console_scripts': [
'command = command.__main__:main'
]
}
)


__main__.py



from . import module

def main():
module.run('arg')


module.py



import shutil

# copy file.txt from command project into the directory where it is running
def run(arg):
shutil.copyfile('../file.txt', './file.txt')


After intalling this package via:



sudo python setup.py install


And calling the program at the command line



$ command


I get the error below



FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'


How can be able to see and use a file like that, that belong to the installed package but i want to use it in an environment where i am running that program?



Edit:



This a simplification of the problem you can download and test:



https://github.com/mctrjalloh/project_initializer










share|improve this question

























  • Is the file present in the installed package? If so, you can use __file__ in a module to find out the absolute path of this module and calculate path to the data file from there

    – Michael Butscher
    Nov 24 '18 at 0:14













  • @MichaelButscher i don't know if it is in there since it has been packaged in a .egg file that i can't open.

    – mctrjalloh
    Nov 24 '18 at 0:23













  • It seemed above that you tried to install the package already. So you should be able to check if the file is installed also.

    – Michael Butscher
    Nov 24 '18 at 0:25











  • I have installed the package, the installation worked without any errors. it's when it come to using it that the error raises. but how can i check if file.txt is in that installed package if that's what you are asking..

    – mctrjalloh
    Nov 24 '18 at 0:27











  • With some kind of file explorer or with the command line to list the files in the installation directory (and subdirectories).

    – Michael Butscher
    Nov 24 '18 at 0:28


















-1















I have created a python package that looks like this:



/command
/command
module.py
__main__.py
README.md
setup.py
file.txt


To install i run:



sudo python setup.py install


Now when i call



$ command


it shows this error:



FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'


There are approximately the contents of modules setup.py, __main__.py and module.py



setup.py



import setuptools

setuptools.setup(
name='command',
...
entry_points={
'console_scripts': [
'command = command.__main__:main'
]
}
)


__main__.py



from . import module

def main():
module.run('arg')


module.py



import shutil

# copy file.txt from command project into the directory where it is running
def run(arg):
shutil.copyfile('../file.txt', './file.txt')


After intalling this package via:



sudo python setup.py install


And calling the program at the command line



$ command


I get the error below



FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'


How can be able to see and use a file like that, that belong to the installed package but i want to use it in an environment where i am running that program?



Edit:



This a simplification of the problem you can download and test:



https://github.com/mctrjalloh/project_initializer










share|improve this question

























  • Is the file present in the installed package? If so, you can use __file__ in a module to find out the absolute path of this module and calculate path to the data file from there

    – Michael Butscher
    Nov 24 '18 at 0:14













  • @MichaelButscher i don't know if it is in there since it has been packaged in a .egg file that i can't open.

    – mctrjalloh
    Nov 24 '18 at 0:23













  • It seemed above that you tried to install the package already. So you should be able to check if the file is installed also.

    – Michael Butscher
    Nov 24 '18 at 0:25











  • I have installed the package, the installation worked without any errors. it's when it come to using it that the error raises. but how can i check if file.txt is in that installed package if that's what you are asking..

    – mctrjalloh
    Nov 24 '18 at 0:27











  • With some kind of file explorer or with the command line to list the files in the installation directory (and subdirectories).

    – Michael Butscher
    Nov 24 '18 at 0:28














-1












-1








-1








I have created a python package that looks like this:



/command
/command
module.py
__main__.py
README.md
setup.py
file.txt


To install i run:



sudo python setup.py install


Now when i call



$ command


it shows this error:



FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'


There are approximately the contents of modules setup.py, __main__.py and module.py



setup.py



import setuptools

setuptools.setup(
name='command',
...
entry_points={
'console_scripts': [
'command = command.__main__:main'
]
}
)


__main__.py



from . import module

def main():
module.run('arg')


module.py



import shutil

# copy file.txt from command project into the directory where it is running
def run(arg):
shutil.copyfile('../file.txt', './file.txt')


After intalling this package via:



sudo python setup.py install


And calling the program at the command line



$ command


I get the error below



FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'


How can be able to see and use a file like that, that belong to the installed package but i want to use it in an environment where i am running that program?



Edit:



This a simplification of the problem you can download and test:



https://github.com/mctrjalloh/project_initializer










share|improve this question
















I have created a python package that looks like this:



/command
/command
module.py
__main__.py
README.md
setup.py
file.txt


To install i run:



sudo python setup.py install


Now when i call



$ command


it shows this error:



FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'


There are approximately the contents of modules setup.py, __main__.py and module.py



setup.py



import setuptools

setuptools.setup(
name='command',
...
entry_points={
'console_scripts': [
'command = command.__main__:main'
]
}
)


__main__.py



from . import module

def main():
module.run('arg')


module.py



import shutil

# copy file.txt from command project into the directory where it is running
def run(arg):
shutil.copyfile('../file.txt', './file.txt')


After intalling this package via:



sudo python setup.py install


And calling the program at the command line



$ command


I get the error below



FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'


How can be able to see and use a file like that, that belong to the installed package but i want to use it in an environment where i am running that program?



Edit:



This a simplification of the problem you can download and test:



https://github.com/mctrjalloh/project_initializer







python include install text-files setup.py






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 4:35







mctrjalloh

















asked Nov 24 '18 at 0:00









mctrjallohmctrjalloh

1398




1398













  • Is the file present in the installed package? If so, you can use __file__ in a module to find out the absolute path of this module and calculate path to the data file from there

    – Michael Butscher
    Nov 24 '18 at 0:14













  • @MichaelButscher i don't know if it is in there since it has been packaged in a .egg file that i can't open.

    – mctrjalloh
    Nov 24 '18 at 0:23













  • It seemed above that you tried to install the package already. So you should be able to check if the file is installed also.

    – Michael Butscher
    Nov 24 '18 at 0:25











  • I have installed the package, the installation worked without any errors. it's when it come to using it that the error raises. but how can i check if file.txt is in that installed package if that's what you are asking..

    – mctrjalloh
    Nov 24 '18 at 0:27











  • With some kind of file explorer or with the command line to list the files in the installation directory (and subdirectories).

    – Michael Butscher
    Nov 24 '18 at 0:28



















  • Is the file present in the installed package? If so, you can use __file__ in a module to find out the absolute path of this module and calculate path to the data file from there

    – Michael Butscher
    Nov 24 '18 at 0:14













  • @MichaelButscher i don't know if it is in there since it has been packaged in a .egg file that i can't open.

    – mctrjalloh
    Nov 24 '18 at 0:23













  • It seemed above that you tried to install the package already. So you should be able to check if the file is installed also.

    – Michael Butscher
    Nov 24 '18 at 0:25











  • I have installed the package, the installation worked without any errors. it's when it come to using it that the error raises. but how can i check if file.txt is in that installed package if that's what you are asking..

    – mctrjalloh
    Nov 24 '18 at 0:27











  • With some kind of file explorer or with the command line to list the files in the installation directory (and subdirectories).

    – Michael Butscher
    Nov 24 '18 at 0:28

















Is the file present in the installed package? If so, you can use __file__ in a module to find out the absolute path of this module and calculate path to the data file from there

– Michael Butscher
Nov 24 '18 at 0:14







Is the file present in the installed package? If so, you can use __file__ in a module to find out the absolute path of this module and calculate path to the data file from there

– Michael Butscher
Nov 24 '18 at 0:14















@MichaelButscher i don't know if it is in there since it has been packaged in a .egg file that i can't open.

– mctrjalloh
Nov 24 '18 at 0:23







@MichaelButscher i don't know if it is in there since it has been packaged in a .egg file that i can't open.

– mctrjalloh
Nov 24 '18 at 0:23















It seemed above that you tried to install the package already. So you should be able to check if the file is installed also.

– Michael Butscher
Nov 24 '18 at 0:25





It seemed above that you tried to install the package already. So you should be able to check if the file is installed also.

– Michael Butscher
Nov 24 '18 at 0:25













I have installed the package, the installation worked without any errors. it's when it come to using it that the error raises. but how can i check if file.txt is in that installed package if that's what you are asking..

– mctrjalloh
Nov 24 '18 at 0:27





I have installed the package, the installation worked without any errors. it's when it come to using it that the error raises. but how can i check if file.txt is in that installed package if that's what you are asking..

– mctrjalloh
Nov 24 '18 at 0:27













With some kind of file explorer or with the command line to list the files in the installation directory (and subdirectories).

– Michael Butscher
Nov 24 '18 at 0:28





With some kind of file explorer or with the command line to list the files in the installation directory (and subdirectories).

– Michael Butscher
Nov 24 '18 at 0:28












2 Answers
2






active

oldest

votes


















0














Only python files are included in the package by default.



To include more add MANIFEST.in and list the file. For example.



Here is a comprehensive tutorial






share|improve this answer
























  • Then how can i reference the file in my code?

    – mctrjalloh
    Nov 24 '18 at 0:44











  • MANIFEST.in is respected by diatutils when you build the package via python setup.py bdist_wheel

    – rikAtee
    Nov 24 '18 at 0:49





















0














So after a lot research i found the solution to this and how it works also. It's a little bit confusing, none of the other stackoverflow answers were really that explanatory. I want to try it here:



I have made a sample project for that only purpose to demostrate and test the solution. I have come up with two solutions: one using the data_files argument of the setup() function and the other using the package_data argument which i preferred the most.



Here is the link to the github repo you can download and test



To use it after installation run



proj init <some-name>


But to be brief there are the most important modules for each method.



USING data_files= ARGUMENT METHOD:



Project structure:



project_initializer
project_initializer
__init__.py
__main__.py
init.py
README.md
setup.py


in setup.py



import setuptools
import os
import sys


PROJECT_NAME = "project_initializer"
DATA_DIR = os.path.join(
sys.prefix, "local/lib/python3.6/dist-packages", PROJECT_NAME)


setuptools.setup(
name='project_initializer',
version='0.1.0',
packages=setuptools.find_packages(),
install_requires=[
'docopt'
],
data_files=[ # is the important part
(DATA_DIR, [
"README.md",
".gitignore"
])
],
entry_points={
'console_scripts': [
'proj = project_initializer.__main__:main'
]
}
)


in init.py



import subprocess
import os
import shutil
import sys

"""Create a new project and initialize it with a .gitignore file
@params project: name of a project to be initialized
effects:
/project
README.md
README.md in the created project directory must be the same as the README.md in THIS directory
"""

PROJECT_NAME = "project_initializer"
DATA_DIR = os.path.join(
sys.prefix, "local/lib/python3.6/dist-packages", PROJECT_NAME)


def run(project):
os.mkdir(project)
shutil.copyfile(os.path.join(DATA_DIR, "README.md"),
f"{project}/README.md") # problem solved


if __name__ == '__main__':
run("hello-world")


USING package_data= ARGUMENT METHOD (which i preferred)



Project structure:



project_initializer
project_initializer
data/
README.md # the file we want to copy
__init__.py
__main__.py
init.py
README.md
setup.py


in setup.py



import setuptools


setuptools.setup(
name='project_initializer',
version='0.1.0',
packages=setuptools.find_packages(),
package_dir={'project_initializer': 'project_initializer'}, # are the ...
package_data={'project_initializer': [ # ... important parameters
'data/README.md', 'data/.gitignore']},
install_requires=[
'docopt'
],
entry_points={
'console_scripts': [
'proj = project_initializer.__main__:main'
]
}
)


in init.py



import subprocess
import os
import shutil
import sys

PROJECT_DIR = os.path.dirname(__file__)

"""Create a new project and initialize it with a .gitignore file
@params project: name of a project to be initialized
effects:
/project
.gitignore
.gitignore in the created project directory must be the same as the gitignore in THIS directory
"""


def run(project):
os.mkdir(project)
shutil.copyfile(os.path.join(PROJECT_DIR, 'data/README.md'),
f"{project}/README.md") # problem solved


if __name__ == '__main__':
run("hello-world")


The reason why i prefer this last method is because you have not to import anything in the setup.py module which could be presumably a bad practice. I guess nothing should be imported in the setup.py file since it is an external file to the main package.



For more detailed explanation of what are the differences between the two arguments check out the python docs



Using data_files= argument



Using package_data= argument






share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454049%2fhow-to-include-a-text-file-in-a-python-installed-package%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Only python files are included in the package by default.



    To include more add MANIFEST.in and list the file. For example.



    Here is a comprehensive tutorial






    share|improve this answer
























    • Then how can i reference the file in my code?

      – mctrjalloh
      Nov 24 '18 at 0:44











    • MANIFEST.in is respected by diatutils when you build the package via python setup.py bdist_wheel

      – rikAtee
      Nov 24 '18 at 0:49


















    0














    Only python files are included in the package by default.



    To include more add MANIFEST.in and list the file. For example.



    Here is a comprehensive tutorial






    share|improve this answer
























    • Then how can i reference the file in my code?

      – mctrjalloh
      Nov 24 '18 at 0:44











    • MANIFEST.in is respected by diatutils when you build the package via python setup.py bdist_wheel

      – rikAtee
      Nov 24 '18 at 0:49
















    0












    0








    0







    Only python files are included in the package by default.



    To include more add MANIFEST.in and list the file. For example.



    Here is a comprehensive tutorial






    share|improve this answer













    Only python files are included in the package by default.



    To include more add MANIFEST.in and list the file. For example.



    Here is a comprehensive tutorial







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 24 '18 at 0:31









    rikAteerikAtee

    4,96553059




    4,96553059













    • Then how can i reference the file in my code?

      – mctrjalloh
      Nov 24 '18 at 0:44











    • MANIFEST.in is respected by diatutils when you build the package via python setup.py bdist_wheel

      – rikAtee
      Nov 24 '18 at 0:49





















    • Then how can i reference the file in my code?

      – mctrjalloh
      Nov 24 '18 at 0:44











    • MANIFEST.in is respected by diatutils when you build the package via python setup.py bdist_wheel

      – rikAtee
      Nov 24 '18 at 0:49



















    Then how can i reference the file in my code?

    – mctrjalloh
    Nov 24 '18 at 0:44





    Then how can i reference the file in my code?

    – mctrjalloh
    Nov 24 '18 at 0:44













    MANIFEST.in is respected by diatutils when you build the package via python setup.py bdist_wheel

    – rikAtee
    Nov 24 '18 at 0:49







    MANIFEST.in is respected by diatutils when you build the package via python setup.py bdist_wheel

    – rikAtee
    Nov 24 '18 at 0:49















    0














    So after a lot research i found the solution to this and how it works also. It's a little bit confusing, none of the other stackoverflow answers were really that explanatory. I want to try it here:



    I have made a sample project for that only purpose to demostrate and test the solution. I have come up with two solutions: one using the data_files argument of the setup() function and the other using the package_data argument which i preferred the most.



    Here is the link to the github repo you can download and test



    To use it after installation run



    proj init <some-name>


    But to be brief there are the most important modules for each method.



    USING data_files= ARGUMENT METHOD:



    Project structure:



    project_initializer
    project_initializer
    __init__.py
    __main__.py
    init.py
    README.md
    setup.py


    in setup.py



    import setuptools
    import os
    import sys


    PROJECT_NAME = "project_initializer"
    DATA_DIR = os.path.join(
    sys.prefix, "local/lib/python3.6/dist-packages", PROJECT_NAME)


    setuptools.setup(
    name='project_initializer',
    version='0.1.0',
    packages=setuptools.find_packages(),
    install_requires=[
    'docopt'
    ],
    data_files=[ # is the important part
    (DATA_DIR, [
    "README.md",
    ".gitignore"
    ])
    ],
    entry_points={
    'console_scripts': [
    'proj = project_initializer.__main__:main'
    ]
    }
    )


    in init.py



    import subprocess
    import os
    import shutil
    import sys

    """Create a new project and initialize it with a .gitignore file
    @params project: name of a project to be initialized
    effects:
    /project
    README.md
    README.md in the created project directory must be the same as the README.md in THIS directory
    """

    PROJECT_NAME = "project_initializer"
    DATA_DIR = os.path.join(
    sys.prefix, "local/lib/python3.6/dist-packages", PROJECT_NAME)


    def run(project):
    os.mkdir(project)
    shutil.copyfile(os.path.join(DATA_DIR, "README.md"),
    f"{project}/README.md") # problem solved


    if __name__ == '__main__':
    run("hello-world")


    USING package_data= ARGUMENT METHOD (which i preferred)



    Project structure:



    project_initializer
    project_initializer
    data/
    README.md # the file we want to copy
    __init__.py
    __main__.py
    init.py
    README.md
    setup.py


    in setup.py



    import setuptools


    setuptools.setup(
    name='project_initializer',
    version='0.1.0',
    packages=setuptools.find_packages(),
    package_dir={'project_initializer': 'project_initializer'}, # are the ...
    package_data={'project_initializer': [ # ... important parameters
    'data/README.md', 'data/.gitignore']},
    install_requires=[
    'docopt'
    ],
    entry_points={
    'console_scripts': [
    'proj = project_initializer.__main__:main'
    ]
    }
    )


    in init.py



    import subprocess
    import os
    import shutil
    import sys

    PROJECT_DIR = os.path.dirname(__file__)

    """Create a new project and initialize it with a .gitignore file
    @params project: name of a project to be initialized
    effects:
    /project
    .gitignore
    .gitignore in the created project directory must be the same as the gitignore in THIS directory
    """


    def run(project):
    os.mkdir(project)
    shutil.copyfile(os.path.join(PROJECT_DIR, 'data/README.md'),
    f"{project}/README.md") # problem solved


    if __name__ == '__main__':
    run("hello-world")


    The reason why i prefer this last method is because you have not to import anything in the setup.py module which could be presumably a bad practice. I guess nothing should be imported in the setup.py file since it is an external file to the main package.



    For more detailed explanation of what are the differences between the two arguments check out the python docs



    Using data_files= argument



    Using package_data= argument






    share|improve this answer






























      0














      So after a lot research i found the solution to this and how it works also. It's a little bit confusing, none of the other stackoverflow answers were really that explanatory. I want to try it here:



      I have made a sample project for that only purpose to demostrate and test the solution. I have come up with two solutions: one using the data_files argument of the setup() function and the other using the package_data argument which i preferred the most.



      Here is the link to the github repo you can download and test



      To use it after installation run



      proj init <some-name>


      But to be brief there are the most important modules for each method.



      USING data_files= ARGUMENT METHOD:



      Project structure:



      project_initializer
      project_initializer
      __init__.py
      __main__.py
      init.py
      README.md
      setup.py


      in setup.py



      import setuptools
      import os
      import sys


      PROJECT_NAME = "project_initializer"
      DATA_DIR = os.path.join(
      sys.prefix, "local/lib/python3.6/dist-packages", PROJECT_NAME)


      setuptools.setup(
      name='project_initializer',
      version='0.1.0',
      packages=setuptools.find_packages(),
      install_requires=[
      'docopt'
      ],
      data_files=[ # is the important part
      (DATA_DIR, [
      "README.md",
      ".gitignore"
      ])
      ],
      entry_points={
      'console_scripts': [
      'proj = project_initializer.__main__:main'
      ]
      }
      )


      in init.py



      import subprocess
      import os
      import shutil
      import sys

      """Create a new project and initialize it with a .gitignore file
      @params project: name of a project to be initialized
      effects:
      /project
      README.md
      README.md in the created project directory must be the same as the README.md in THIS directory
      """

      PROJECT_NAME = "project_initializer"
      DATA_DIR = os.path.join(
      sys.prefix, "local/lib/python3.6/dist-packages", PROJECT_NAME)


      def run(project):
      os.mkdir(project)
      shutil.copyfile(os.path.join(DATA_DIR, "README.md"),
      f"{project}/README.md") # problem solved


      if __name__ == '__main__':
      run("hello-world")


      USING package_data= ARGUMENT METHOD (which i preferred)



      Project structure:



      project_initializer
      project_initializer
      data/
      README.md # the file we want to copy
      __init__.py
      __main__.py
      init.py
      README.md
      setup.py


      in setup.py



      import setuptools


      setuptools.setup(
      name='project_initializer',
      version='0.1.0',
      packages=setuptools.find_packages(),
      package_dir={'project_initializer': 'project_initializer'}, # are the ...
      package_data={'project_initializer': [ # ... important parameters
      'data/README.md', 'data/.gitignore']},
      install_requires=[
      'docopt'
      ],
      entry_points={
      'console_scripts': [
      'proj = project_initializer.__main__:main'
      ]
      }
      )


      in init.py



      import subprocess
      import os
      import shutil
      import sys

      PROJECT_DIR = os.path.dirname(__file__)

      """Create a new project and initialize it with a .gitignore file
      @params project: name of a project to be initialized
      effects:
      /project
      .gitignore
      .gitignore in the created project directory must be the same as the gitignore in THIS directory
      """


      def run(project):
      os.mkdir(project)
      shutil.copyfile(os.path.join(PROJECT_DIR, 'data/README.md'),
      f"{project}/README.md") # problem solved


      if __name__ == '__main__':
      run("hello-world")


      The reason why i prefer this last method is because you have not to import anything in the setup.py module which could be presumably a bad practice. I guess nothing should be imported in the setup.py file since it is an external file to the main package.



      For more detailed explanation of what are the differences between the two arguments check out the python docs



      Using data_files= argument



      Using package_data= argument






      share|improve this answer




























        0












        0








        0







        So after a lot research i found the solution to this and how it works also. It's a little bit confusing, none of the other stackoverflow answers were really that explanatory. I want to try it here:



        I have made a sample project for that only purpose to demostrate and test the solution. I have come up with two solutions: one using the data_files argument of the setup() function and the other using the package_data argument which i preferred the most.



        Here is the link to the github repo you can download and test



        To use it after installation run



        proj init <some-name>


        But to be brief there are the most important modules for each method.



        USING data_files= ARGUMENT METHOD:



        Project structure:



        project_initializer
        project_initializer
        __init__.py
        __main__.py
        init.py
        README.md
        setup.py


        in setup.py



        import setuptools
        import os
        import sys


        PROJECT_NAME = "project_initializer"
        DATA_DIR = os.path.join(
        sys.prefix, "local/lib/python3.6/dist-packages", PROJECT_NAME)


        setuptools.setup(
        name='project_initializer',
        version='0.1.0',
        packages=setuptools.find_packages(),
        install_requires=[
        'docopt'
        ],
        data_files=[ # is the important part
        (DATA_DIR, [
        "README.md",
        ".gitignore"
        ])
        ],
        entry_points={
        'console_scripts': [
        'proj = project_initializer.__main__:main'
        ]
        }
        )


        in init.py



        import subprocess
        import os
        import shutil
        import sys

        """Create a new project and initialize it with a .gitignore file
        @params project: name of a project to be initialized
        effects:
        /project
        README.md
        README.md in the created project directory must be the same as the README.md in THIS directory
        """

        PROJECT_NAME = "project_initializer"
        DATA_DIR = os.path.join(
        sys.prefix, "local/lib/python3.6/dist-packages", PROJECT_NAME)


        def run(project):
        os.mkdir(project)
        shutil.copyfile(os.path.join(DATA_DIR, "README.md"),
        f"{project}/README.md") # problem solved


        if __name__ == '__main__':
        run("hello-world")


        USING package_data= ARGUMENT METHOD (which i preferred)



        Project structure:



        project_initializer
        project_initializer
        data/
        README.md # the file we want to copy
        __init__.py
        __main__.py
        init.py
        README.md
        setup.py


        in setup.py



        import setuptools


        setuptools.setup(
        name='project_initializer',
        version='0.1.0',
        packages=setuptools.find_packages(),
        package_dir={'project_initializer': 'project_initializer'}, # are the ...
        package_data={'project_initializer': [ # ... important parameters
        'data/README.md', 'data/.gitignore']},
        install_requires=[
        'docopt'
        ],
        entry_points={
        'console_scripts': [
        'proj = project_initializer.__main__:main'
        ]
        }
        )


        in init.py



        import subprocess
        import os
        import shutil
        import sys

        PROJECT_DIR = os.path.dirname(__file__)

        """Create a new project and initialize it with a .gitignore file
        @params project: name of a project to be initialized
        effects:
        /project
        .gitignore
        .gitignore in the created project directory must be the same as the gitignore in THIS directory
        """


        def run(project):
        os.mkdir(project)
        shutil.copyfile(os.path.join(PROJECT_DIR, 'data/README.md'),
        f"{project}/README.md") # problem solved


        if __name__ == '__main__':
        run("hello-world")


        The reason why i prefer this last method is because you have not to import anything in the setup.py module which could be presumably a bad practice. I guess nothing should be imported in the setup.py file since it is an external file to the main package.



        For more detailed explanation of what are the differences between the two arguments check out the python docs



        Using data_files= argument



        Using package_data= argument






        share|improve this answer















        So after a lot research i found the solution to this and how it works also. It's a little bit confusing, none of the other stackoverflow answers were really that explanatory. I want to try it here:



        I have made a sample project for that only purpose to demostrate and test the solution. I have come up with two solutions: one using the data_files argument of the setup() function and the other using the package_data argument which i preferred the most.



        Here is the link to the github repo you can download and test



        To use it after installation run



        proj init <some-name>


        But to be brief there are the most important modules for each method.



        USING data_files= ARGUMENT METHOD:



        Project structure:



        project_initializer
        project_initializer
        __init__.py
        __main__.py
        init.py
        README.md
        setup.py


        in setup.py



        import setuptools
        import os
        import sys


        PROJECT_NAME = "project_initializer"
        DATA_DIR = os.path.join(
        sys.prefix, "local/lib/python3.6/dist-packages", PROJECT_NAME)


        setuptools.setup(
        name='project_initializer',
        version='0.1.0',
        packages=setuptools.find_packages(),
        install_requires=[
        'docopt'
        ],
        data_files=[ # is the important part
        (DATA_DIR, [
        "README.md",
        ".gitignore"
        ])
        ],
        entry_points={
        'console_scripts': [
        'proj = project_initializer.__main__:main'
        ]
        }
        )


        in init.py



        import subprocess
        import os
        import shutil
        import sys

        """Create a new project and initialize it with a .gitignore file
        @params project: name of a project to be initialized
        effects:
        /project
        README.md
        README.md in the created project directory must be the same as the README.md in THIS directory
        """

        PROJECT_NAME = "project_initializer"
        DATA_DIR = os.path.join(
        sys.prefix, "local/lib/python3.6/dist-packages", PROJECT_NAME)


        def run(project):
        os.mkdir(project)
        shutil.copyfile(os.path.join(DATA_DIR, "README.md"),
        f"{project}/README.md") # problem solved


        if __name__ == '__main__':
        run("hello-world")


        USING package_data= ARGUMENT METHOD (which i preferred)



        Project structure:



        project_initializer
        project_initializer
        data/
        README.md # the file we want to copy
        __init__.py
        __main__.py
        init.py
        README.md
        setup.py


        in setup.py



        import setuptools


        setuptools.setup(
        name='project_initializer',
        version='0.1.0',
        packages=setuptools.find_packages(),
        package_dir={'project_initializer': 'project_initializer'}, # are the ...
        package_data={'project_initializer': [ # ... important parameters
        'data/README.md', 'data/.gitignore']},
        install_requires=[
        'docopt'
        ],
        entry_points={
        'console_scripts': [
        'proj = project_initializer.__main__:main'
        ]
        }
        )


        in init.py



        import subprocess
        import os
        import shutil
        import sys

        PROJECT_DIR = os.path.dirname(__file__)

        """Create a new project and initialize it with a .gitignore file
        @params project: name of a project to be initialized
        effects:
        /project
        .gitignore
        .gitignore in the created project directory must be the same as the gitignore in THIS directory
        """


        def run(project):
        os.mkdir(project)
        shutil.copyfile(os.path.join(PROJECT_DIR, 'data/README.md'),
        f"{project}/README.md") # problem solved


        if __name__ == '__main__':
        run("hello-world")


        The reason why i prefer this last method is because you have not to import anything in the setup.py module which could be presumably a bad practice. I guess nothing should be imported in the setup.py file since it is an external file to the main package.



        For more detailed explanation of what are the differences between the two arguments check out the python docs



        Using data_files= argument



        Using package_data= argument







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 3:39

























        answered Nov 26 '18 at 3:18









        mctrjallohmctrjalloh

        1398




        1398






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454049%2fhow-to-include-a-text-file-in-a-python-installed-package%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()