Broken setup.py implementation for a minimal module - python

I forked the nice module multiscorer and I am trying to turn it into a package that I could install in different environments.
My fork can be found here. The steps I took are
Create a new environment (using conda) and activate it
python setup.py install from the root directory of the fork
In a new terminal, activate the environment and move to some arbitrary location. Start ipython and try from multiscorer import MultiScorer
I get the following error ImportError: cannot import name 'MultiScorer'. Note however, that import multiscorer works just fine. What do I have to change in the code to enable an installation using python setup.py install?
Another attempt: I tried to replace packages=['multiscorer'] with py_modules=['multiscorer.multiscorer']. Didn't help...

Your setup.py is alright. The problem is the package structure. Right now the correct way to import the Multiscorer class is this: from multiscorer.multiscorer import Multiscorer. The first multiscorer is for the folder (package) of the same name and the second multiscorer is for the multiscorer.py module inside the package.
The docs recommend putting all your code inside the __init__.py for such small packages.
If your codebase later grows too large for one file, you can start introducing other modules and use the __init__.py for exposing classes/functions on the package level.
Hope this helps.

It turns out I tried to import the wrong thing. The following: from multiscorer.multiscorer import MultiScorer works.
I am now wondering is this the pythonic way.

Related

Configuring import errors while installing the project

I am installing the Django==1.9.1 project of the former team (I am newcomer) to my server it runs on Centos7.
The project structure is:
agros>
address.json
agros>
agros.ini
agros.log
apps>
insurance>
mixins.py
user>
models.py
I did as I usually do: installed all the required things and run:
pip install -r requrements.txt
Then I run the following code:
python manage.py makemigrations
The problematic package is imported in this way
from apps.insurance.mixins import .......
But I am receiving the following error
ImportError: No module name insurance.mixins
insurance.mixins is my library. I checked and it is where it should be. What can I do to fix this!
pip install usually installs from PyPi. insurance looks like a custom module. This wouldn't be in PyPi. So find where insurance module is and copy it to the proper location. If you don't know where to copy the module run ,
import sys
sys.path
and check site-packages is located. And then move your insurance module over there. You can also move your insurance package to any of the paths returned by sys.path as it checks all of them. You can also add new paths to this list.
Hopefully that should fix the problem.

Python: No Module named xxx

Im getting nowhere with the following error on my Raspberry Pi:
My own Python script calls a function from another module named BlackBean.py which in turn imports other modules called "netaddr" and "configparser". The problem is that I just cant seem to get past the import error which tells me " No Module named netaddr, or if I comment out that import then it also errors with No Module named configparser. So I know its a path issue but I just cant seem to get it fixed!
The Blackbean.Py script starts like this:
import broadlink
import ConfigParser
import sys, getopt
import time, binascii
import netaddr
import BlackBeanSettings
import re
from os import path
from Crypto.Cipher import AES
SettingsFile = ConfigParser.ConfigParser()
SettingsFile.optionxform = str
SettingsFile.read(BlackBeanSettings.BlackBeanControlSettings)
def execute_command(etc.........
The BlackBean.py file is in my project SkyHD folder at /home/pi/SkyHD.
The "netaddr" and "configparser" files & folders were installed by pip in /home/pi/.local/lib/python2.7(and python3.5)/site-package folders.
sys.path has the above folders in its list and Ive also edited .bashrc and added PYTHONPATH=${PYTHONPATH}:/home/pi/.local/lib/python2.7/site-package:/home/pi/.local/lib/python3.5/site-package:/home/pi/SkyHD:../
but none of this works. I guess it must be something basic but I just cant work it out! help!
Also, some more info, when I first install all the files and run my program everything works fine and it finds the files ok with no problems, its only when I reboot it fails to find the files.
Its fixed.
Python looks for imported modules in 3 places, the first being the folder you launched the python script from; so for me the obvious answer is to import the modules I need directly into my own Project folder (/home/pi/myproject). This worked just fine, it works every time even after reboot, which was my main problem before. No need to create or alter PYTHONPATH, no need to mess around with entries in .bashrc or try to change the python path entries. Here are the steps:
Upgrade PIP to version 9.0.3 (not ver 10) with
pip install --upgrade pip==9.0.3
then install the required modules with the following
pip install --target=/home/pi/your_project_folder module_name
so for me it was... pip install --target=/home/pi/SkyHD netaddr
Im sure this is not best practice, but my Raspberry Pi only has this one project to run and having modules imported into the Projects folder just isnt an issue.
Hope this helps some others with the same problem.
You've provided insufficient information. Specifically, details about the python command being used to run your script such as its version (python -V) and its module search path if you do
env -u PYTHONPATH python -c 'import sys; print(sys.path);'
Similarly you can easily simplify the problem. What happens if you do python -m netaddr?
Obviously in the above commands substitute the actual python command being used to run your script.
And, as #BoarGules mentioned in his comments to your question, you should never, ever add directories to PYTHONPATH for different python versions unless you know that the modules in those directories has been written to work with python2 and python3.

How to import a module from PyPI when I have another module with the same name

I'm trying to use the lockfile module from PyPI. I do my development within Spyder. After installing the module from PyPI, I can't import it by doing import lockfile. I end up importing anaconda/lib/python2.7/site-packages/spyderlib/utils/external/lockfile.py instead. Spyder seems to want to have the spyderlib/utils/external directory at the beginning of sys.path, or at least none of the polite ways I can find to add my other paths get me in front of spyderlib/utils/external.
I'm using python2.7 but with from __future__ import absolute_import.
Here's what I've already tried:
Writing code that modifies sys.path before running import lockfile. This works, but it can't be the correct way of doing things.
Circumventing the normal mechanics of importing in Python using the imp module (I haven't gotten this to work yet, but I'm guessing it could be made to work)
Installing the package with something like pip install --install-option="--prefix=modules_with_name_collisions" package_name. I haven't gotten this to work yet either, but I'm guess it could be made to work. It looks like this option is intended to create an entirely separate lib tree, which is more than I need. Source
Using pip install --target=lockfile_from_pip. The files show up in the directory where I tell them to go, but import doesn't find them. And in fact pip uninstall can't find them either. I get Cannot uninstall requirement lockfile-from-pip, not installed and I guess I will just delete the directories and hope that's clean. Source
So what's the preferred way for me to get access to the PyPI lockfile module?

Explain why numpy should not be imported from source directory

Disclaimer of research:
I have examined the following other StackOverflow questions:
How to import numpy in python shell
How can I use numpy without installing it?
Import a module from a relative path
Perhaps to some, those may answer my question, but according to my knowledge, I still do not understand the situation.
I am trying to import numpy so that matplotlib will work, but upon execution of the __init__.py file in the numpy folder, the following error message is displayed:
ImportError: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python intepreter from there.
Explain what it means to import something from its source directory as opposed to some other way of importing it. Does it mean that it should not be source code when it is imported? Or does it mean that it literally is just the wrong directory/folder that I am importing. I know that one other StackOverflow answer is:
The message is fairly self-explanatory; your working directory should not be the numpy source directory when you invoke Python; numpy should be installed and your working directory should be anything but the directory where it lives.
However, I don't understand this. Aren't you supposed to import things that you want to work with? I'm assuming that the import command combines the source directory into your current working directory in this statement.
I also read the other answers such as:
Using distutils to install local directories
Using virtualenv to create a virtual system directory
Using Enthought's EPD to have numpy pre-installed in what I believe to be the system directory,
and
Using a command like $ dpkg -i --force-not-root --root=$HOME mypackagename.deb to create what I believe is some kind of sub-system directory that is treated like a system directory.
So, correct me if I'm wrong, but does numpy somehow strongly require to be somehow installed in the main system directory?
Machine status:
I am using Windows machines without administrative privlidges.
They have Python 3.3 Shell as well as matplotlib installed.
When running command prompt, python and python3 are not recognized. I have to run the Python shell from the applications menu.
I can successfull begin importing matplotlib from even my own directory, different from theirs, but it stops upon reaching __init__.py of the numpy module, if it exists and reports the error stated above.
Update:
Luckily, my administrators were able to directly install numpy correctly in the site-packages folder. Thank you for answering my question though. I understand the situation a lot more because of you.
numpy includes extension modules written in C. You will need to build these extension modules before the numpy package is complete. The most robust way to do this is to build it and install it to site-packages like normal. You can also install it to another directory using the standard distutils options for this. However, once you have installed it, you should change your directory out of the source tree. Python starts looking for packages in your current directory, so the presence of the incomplete numpy package (without the necessary built C extension modules) will be picked up first and lead to the error that message that you quote. This happens a lot, so we give a long message explaining what to do.

Python 2.4 Doesn't Load Home-Directory Packages

I'm trying to get a Python package to install to my home directory because I don't have the privileges to install it system-wide.
The package is PyProj, and I am trying to install it using python setup.py install --home=~ (with Python 2.4.3), as recommended in the Python documentation. The package compiles successfully and copies itself to what I assume are the correct directories (the directory ~/lib64/python/pyproj appears during install).
But, when I load Python up and type import pyproj, I'm told ImportError: No module named pyproj.
Any thoughts on what might be going on?
You'll need to set PYTHONPATH to tell Python where to locate your locally installed packages.
For example:
[you#home]$ export PYTHONPATH="~/lib64/python"
Or, to do this within the interpreter (or script):
import sys, os
sys.path.append(os.path.expanduser("~/lib64/python"))
For more information on how Python locates installed modules, see section on The Module search Path in docs.
~/lib64/python/pyproj is not part of your PYTHONPATH. There are two or three ways around this, depending on your needs.
The first is to directly modify the path in your module, suitable if you're only going to use it from one module. As noted in the comments, this method does not do expansion on the '~' character.
import sys
sys.path.append('/home/username/lib64/python')
import pyproj
The second way is to add ~/lib64/python/pyproj to your system's PYTHONPATH, through whatever method your system suggests. A line in .bash_profile is shown below.
export PYTHONPATH=$PYTHONPATH:~/lib64/python/pyproj
See the Python Documentation for more details.

Categories