I'm having a problem after having recently switched from python2 to python3.8.
This is my file setup (abridged):
package/
__init__.py
main.py
modules/
__init__.py
log.py
In python2 I used to import log.py into main.py with
import modules.log
When I do this in python3 and call main.py from the console with
python /path/to/package/main.py
everything works fine.
But then I create a python package from that and install it on my archlinux/raspberrypi and call the script via the console with
/usr/bin/package
it tells me
$ package
Traceback (most recent call last):
File "/usr/bin/package", line 11, in <module>
load_entry_point('package==3.1.1', 'console_scripts', 'package')()
File "/usr/lib/python3.8/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2852, in load_entry_point
return ep.load()
File "/usr/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2443, in load
return self.resolve()
File "/usr/lib/python3.8/site-packages/pkg_resources/__init__.py", line 2449, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/usr/lib/python3.8/site-packages/package/main.py", line 5, in <module>
import modules.log
ModuleNotFoundError: No module named 'modules'
Any idea why it doesn't work? I suppose it looks in different places once installed as a package.
Thanks in advance
EDIT: I can successfully run the package when I change the imports to
from package.modules import log
But then it will only work if it is installed as a package. Running it from my IDE (where it is not installed as a package) will naturally result in the error
ModuleNotFoundError: No module named 'package'
You could use relative imports for intra-package references, e.g. for main.py referencing log.py it would be:
import .modules.log
There have been huge changes in import behavior between Python 2 and 3. Refer to The Conservative Python 3 Porting Guide: Importing
For local development purposes there is a nice option to install package as a symlink
pip install -e ./ --user
In that case it will keep your installed package up-to-date with source, while you will be able to use this package as usual.
You need to specify what is root of your source files to packaging utitlity.
I expectiong you have standard setup.py with your package definition.
Look at package_dir option
https://python.readthedocs.io/en/stable/distutils/examples.html
(Alternatively you can change PYTHONPATH in you ide configuration and use package.modules)
Related
I'm using python version 3.6.1
I want to use method of one python file to another located in a same directory. I have used from utils import wit_response in app.py file. And when I compile it this shows an error:
Traceback (most recent call last):
File "app.py", line 4, in
from utils import wit_response
File "E:\Study\Python\fbmessengerbot\utils.py", line 1, in
from wit import Wit
ModuleNotFoundError: No module named 'wit'
In utils.py file I'm using from wit import Wit. The wit package i have already installed.
How can i resolve this.
Thanks in advance.
If you are using a Virtual environment do pip freeze and check whether "wit" package is installed or not.
If "wit" is installed open ipython and check whether you can import the "wit" package.
If "wit" is not imported in ipython check if any dependency packages are need for "wit".
Hi !
I've tried to use a package I've created (named mypackage) on my main program (named collector). mypackage is defined with the setup.py below:
name = "mypackage"
[...]
packages = find_packages(),
include_package_data = True,
exclude_package_data = { '': ['README.md'] }
[...]
entry_points = {
'console_scripts': [
'my-package=mypack.mypack:main'
]
},
I want to use a little program present this package, which will normally be called as below on collector:
from mypack.common.prog import mylittleprogram
I've tried to install mypackage on a virtualenv, and then (re)installed my main program. Then I executed it, and there is the error I have:
Traceback (most recent call last):
from project.common.collector import collectArt
File "/root/project/lib/python3.5/site-packages/collector-0.1-py3.5.egg/project/common/collector.py", line 7, in <module>
ImportError: No module named 'mypack'
Also, when the virtualenv is activated, I've just tried to launch the entry-point named my-package:
Traceback (most recent call last):
File "/root/project/bin/my-package", line 11, in <module>
load_entry_point('mypackage==0.1.1', 'console_scripts', 'my-package')()
File "/root/project/lib/python3.5/site-packages/pkg_resources/__init__.py", line 560, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/root/project/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2648, in load_entry_point
return ep.load()
File "/root/project/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2302, in load
return self.resolve()
File "/root/project/lib/python3.5/site-packages/pkg_resources/__init__.py", line 2308, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
ImportError: No module named 'mypack'
Note 1: I precise that I have the __init__.py on each folder of my package.
Note 2: the virtualenv folders are on the same folder as my main program.
How do I use mylittleprogram present on mypackage on my collector main program with virtualenv ? Did I made something wrong ?
Thank you :)
There is the issue, I had to install my package via the following command:
python setup.py develop
And then the package is recognized. This command has to be executed when the virtualenv is activated, and be executed on the directory of the package.
Hope this will help someone in the future !
I'm working on a project to play around with a csv file, however, I can't get pandas to work. Everything I have researched so far has just told me to make sure that pandas is installed. Using pip I have managed to install pandas just fine. When I try to run my python program and import pandas I get:
Traceback (most recent call last):
File "analysis.py", line 1, in <module>
import pandas as pd
ImportError: No module named pandas
I don't know if this is related, but I was trying to see see if this would work with conda install pandas however, I get this import error:
(Again, Anaconda is correctly installed on my system)
Traceback (most recent call last):
File "/usr/local/bin/conda", line 9, in <module>
load_entry_point('conda==4.2.7', 'console_scripts', 'conda')()
File "/Library/Python/2.7/site-packages/pkg_resources/__init__.py", line 561, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/Library/Python/2.7/site-packages/pkg_resources/__init__.py", line 2627, in load_entry_point
return ep.load()
File "/Library/Python/2.7/site-packages/pkg_resources/__init__.py", line 2287, in load
return self.resolve()
File "/Library/Python/2.7/site-packages/pkg_resources/__init__.py", line 2293, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/Library/Python/2.7/site-packages/conda/cli/__init__.py", line 8, in <module>
from .main import main # NOQA
File "/Library/Python/2.7/site-packages/conda/cli/main.py", line 46, in <module>
from ..base.context import context
File "/Library/Python/2.7/site-packages/conda/base/context.py", line 18, in <module>
from ..common.configuration import (Configuration, MapParameter, PrimitiveParameter,
File "/Library/Python/2.7/site-packages/conda/common/configuration.py", line 40, in <module>
from ruamel.yaml.comments import CommentedSeq, CommentedMap # pragma: no cover
ImportError: No module named ruamel.yaml.comments
Anyone have a similar experience importing libraries? I have tried to make sure that I have the correct versions for Python3 but that doesn't seem to be the issue. Any help would be greatly appreciated! Thank you.
The problem is that the 'lib' folder that pip installs things into has a separate subdirectory for each python environment that you're working with (by default this is python2.7). A simple workaround is to build a virtual environment of python3 and then installing pandas or whatever package you want into it. To achieve this, try the following:
virtualenv -p /usr/bin/python3 python3
Now, each time you want to pip install a python3 package, run
source python3/bin/activate
pip install pandas --user
Hope this helps
Go to powershell on your system and pip install pandas. you will see a successfully installed command. you can install other modules too like matplotlib et al.
I am running python based software that requires a virtualenv with various additional python packages installed therein. Here are the install instructions:
Clone the repository to a shared filesysem on a cluster
>git clone https://github.com/jgurtowski/nanocorr
>cd nanocorr
Create a virtual environment to install python dependencies
>virtualenv nanocorr_ve
>source nanocorr_ve/bin/activate
install the following packages using pip:
pip install git+https://github.com/cython/cython
pip install numpy
pip install h5py
pip install git+https://github.com/jgurtowski/pbcore_python
pip install git+https://github.com/jgurtowski/pbdagcon_python
pip install git+https://github.com/jgurtowski/jbio
pip install git+https://github.com/jgurtowski/jptools
#Finally install the nanocorr package itself
> python setup.py install
I achieved this with no problems. However, While running the software, I found that it was failing with the following traceback.
(nanocorr_ve)[nanocorr_test]$ correctOxford
Traceback (most recent call last):
File "/local/workdir/malonge/nanocorr_test/nanocorr/nanocorr_ve/bin/correctOxford", line 9, in <module>
load_entry_point('jptools==0.1', 'console_scripts', 'correctOxford')()
File "/local/workdir/malonge/nanocorr_test/nanocorr/nanocorr_ve/lib/python2.7/site- packages/pkg_resources/__init__.py", line 558, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/local/workdir/malonge/nanocorr_test/nanocorr/nanocorr_ve/lib/python2.7/site- packages/pkg_resources/__init__.py", line 2682, in load_entry_point
return ep.load()
File "/local/workdir/malonge/nanocorr_test/nanocorr/nanocorr_ve/lib/python2.7/site- packages/pkg_resources/__init__.py", line 2355, in load
return self.resolve()
File "/local/workdir/malonge/nanocorr_test/nanocorr/nanocorr_ve/lib/python2.7/site- packages/pkg_resources/__init__.py", line 2361, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/local/workdir/malonge/nanocorr_test/nanocorr/nanocorr_ve/lib/python2.7/site- packages/jptools/correct.py", line 21, in <module>
from pbtools.pbdagcon.q_sense import output_dag_info
File "/local/workdir/malonge/nanocorr_test/nanocorr/nanocorr_ve/lib/python2.7/site- packages/pbtools/pbdagcon/q_sense.py", line 50, in <module>
from pbcore.io import FastaReader
File "/programs/python/lib/python2.7/site-packages/pbcore-1.0.0- py2.7.egg/pbcore/io/__init__.py", line 31, in <module>
from .BasH5IO import *
File "/programs/python/lib/python2.7/site-packages/pbcore-1.0.0- py2.7.egg/pbcore/io/BasH5IO.py", line 37, in <module>
import h5py, numpy as np, os.path as op
File "/programs/python/lib/python2.7/site-packages/h5py-2.5.0-py2.7-linux- x86_64.egg/h5py/__init__.py", line 13, in <module>
from . import _errors
ImportError: /programs/python/lib/python2.7/site-packages/h5py-2.5.0-py2.7-linux-x86_64.egg/h5py/_errors.so: undefined symbol: PyUnicodeUCS2_DecodeUTF8
I have found details as to the specific error itself, but I think the source of the problem is that the traceback clearly shows a transition from modules being imported from the /local virtualenv site-packages to another site-packages directory in /programs.
My question is the following: Given that I have the same python packages installed in two separate places, 1 in the system wide location and one in virtualenv, how do I ensure that other programs properly import all modules from within the same virtualenv sit-packages directory. I thought this would have been done automatically. I am not 100% sure that this is the sole reason for the error, but being able to fix this would help me to further debug.
It depends on how you set up your env. virtualenv has a flag option that forces it to not use the system site packages
virtualenv --no-site-packages my_env
It is the default behavior for recent versions (>=1.7).
If you've defined a PYTHONPATH environment variable, that will affect your virtualenv environment as well.
I'm trying to get a work project that I git-cloned from work (used in numerous other places, I know the project works fine) to run on my MacBook. I installed Django (via sudo python setup.py install), as well as MySQL, the Python MySQL wrapper, and also virtualenv with virtualenvwrapper.
After creating a virtualenv for my project (virtualenv is created fine), and doing a pip install for the project requirements (all of which install into the proper virtualenv directory), an attempt to do a runserver command spits out the following:
Traceback (most recent call last):
File "manage.py", line 12, in <module>
execute_manager(settings)
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 436, in execute_manager
setup_environ(settings_mod)
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 419, in setup_environ
project_module = import_module(project_name)
File "/Library/Python/2.6/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
ImportError: No module named website-backend
First try to import this library from python or ./manage.py shell.
If that fails import sys and check if your lib is in sys path print sys.path