简体   繁体   中英

install python package to sys.path

I am new to python3 and I want to create a package that I can import from other python script.

So I created my package and I run

python3 setup.py sdist

to create my tarball.

when I move it to another directory to untar it and then install the package with

python3 setup.py install -user --prefix=

it's fine there is no error and it install in my site-package of python and when I run python3 I can import my lib and call its function.

But when I want to import this package in a script it tell me

ImportError: No module named test_package.pck1.addition

I know that I can add

import sys
sys.path.append('./test_package.0.1')

to fix it but I want to avoid that because it would mean always having to modify the path to the package when trying to use it in another script.

So How can I import my package without modifying the sys.path. Or how can I an install my package so that my script won't need that.

I tried a lot of installation process (using pip3, trying different option etc) nothing work as I expect it.

you created the python package 'toto.py'. you want to import it in the python script 'tata.py'. you write 'import toto' in the tata script and you place the file 'toto.py' in a directory contained in the PYTHONPATH environment variable.

Although I don't use python 2, it seems to me it is the same for python 2 and 3. Thus I'm not sure I understood the question.

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