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.
Related
I'm working with Django in a project, I want to use a python function from a project in Github, I can't install this project as a library with pip install dicom-exporter because it doesn' work.
The project I want to use:
https://github.com/KitwareMedical/dicom-exporter/
the function I want to use :
dicom-exporter <path/to/dicom/folder> <path/to/output.vti>
The project contain setup.py file where there is script entry-point,
Please How can use this project in my django project.
there is a simple way to do this in python
make sure that the file you want to import your function from is in the same directory as your project
make sure that the file you are importing the function from isn't named like an already existing python module
you can import the function into your project by writing into your project:
from [name of the file that contains the function] import [name of the function you want to import]
In this very case though you can simply type in your terminal pip install . as stated here
Its very simple as the Readme file in the project says you would use pip install that ends with a dot. But you will need to open the where you downloaded the project, in its directory contains the Setup.py in your command prompt in other to use the [pip install .], so make sure to download the project clone as a zip file and unzip in to the directory where you will find it in your command prompt and run the pip install .
#First navigate to where the project directory contains the setup.py file in your
# command prompt and run pip install that ends with a dot
pip install .
For a class, I need to be able to use this github library: https://github.com/matsselen/pyolab. I am struggling to download/install it in a way that actually works. I am using anaconda for this and commands I've tried include:
conda install pyolab
pip install pyolab
conda install source_code_file_path
pip install -e git+https://github.com/matsselen/pyolab#egg=pyolab
I've saved the source code into anaconda's 'pkgs' folder and in my root folder.
I really don't know much about creating packages but in my searching, I found that the issue might be that there is no setup.py file included in the code on github. I tried to build my own but I can't get that to work. Here is the code I have for that:
from setuptools import setup
setup(name='pyolab',
version='master',
description='IOLab code'
author='mats selen'
packages=['pyolab']
)
Also, I am being required to use the python 2.7 version of the package instead of the newer python 3 version.
Can anyone help point me in the right direction to get this working?
No need to pip-install repository, any python scripts can be used directly from any regular files/dirs on file system.
Just clone the repository:
git clone https://github.com/matsselen/pyolab
and then use sys.path to specify location of library's scripts and import them:
import sys
sys.path.append('./pyolab/PyOLabCode/')
# all dirs from sys.path are scanned by Python when you do import
# and the first matched dir where the module is found is used
import commMethods # importing a script ./pyolab/PyOLabCode/commMethods.py
I have a pytest test, let's call it test.py. I used to run this test outside of virtualenv; now I'm trying to run it inside a virtualenv sandbox.
The project is structured like this:
~/project/test # where test.py and all virtualenv files live
~/project/mylibrary
test.py imports from mylibrary. In the past, this worked because I have the code in ~/project/mylibrary installed into /usr/lib/python2.7/dist-packages/mylibrary.
I can't run virtualenv with the --system-site-packages flag. I also can't move the code from ~/project/mylibrary into the ~/project/test folder. How can I get access to the code in mylibrary inside my virtualenv?
You don't need to do anything special - as long as you are working inside a virtualenv, python setup.py install will automatically install packages into
$VIRTUAL_ENV/lib/python2.7/site-packages
rather than your system-wide
/usr/lib/python2.7/dist-packages
directory.
In general it's better to use pip install mylibrary/, since this way you can neatly uninstall the package using pip uninstall mylibrary.
If you're installing a working copy of some code that you're developing, it might be a good idea to install it in "editable" mode using pip install -e mylibrary/, which creates a link to your source directory so that your installed module gets updated as you edit the code.
The easiest way would be to add the directory containing the library to your sys.path
I try the following code to see if the library sodium can be located
import ctypes
import ctypes.util
# Taken from line 33 https://github.com/bgaifullin/pysodium/blob/master/pysodium/__init__.py
o = ctypes.util.find_library('sodium')
print o
This always returns "none"
Please how do I add external libraries (dependencies) and reference them correctly in my python code.
EDIT:
I am trying to work with pysodium it has a dependency on libsodium
I have downloaded libsodium, but i'm new to python...
I'm actually using PTVS 2.1 to get up to speed running python in my familiar dev environment.
If I understood you correctly. What you want is to import a library.
Put the pysodium directory under the script you want to use and then simply do
import pysodium
It is as simple as that.
Usually, what you do is install the libraries on your system, or in a virtualenv, and import them to your python script. Cloning the repository will not generally help unless the libraries you want to import are in the same directory as the script you're importing from.
I, personally, would recommend using virtualenv and pip together hand in hand. Read up on virtualenv, it will come very handy.
Assuming you have both virtualenv and pip, all you need to do is the following
virtualenv venv
source venv/bin/activate
pip install pysodium
This should create a virtualenv container, activate it and install pysodium inside. Your script will only work when the virtualenv is activated. You can deactivate it using the command deactivate.
I have built a module using "conda build packagename".
However, the built module ends up in "\Anaconda\conda-bld\work".
The module can only be imported (using "import packagename")if I cd into this directory, then run Python. I have tried placing the files in "\Anaconda\conda-bld\work" in "\Anaconda\Lib\site-packages", however I am not able to import the module from any directory; I must be in "\Anaconda\Lib\site-packages".
Is the only solution to put the .PYD file/ .SO file next to the executable Python file or is there a way to let Python know there is a new module installed?
Thank you for your help.
In the conda build script, you need to install the files, not just build them. For Python, this typically means running python setup.py install in the build.sh, and including python in your build dependencies so that the python will install into the build environment.