I have made a Python 3 script for testing a project of mine. The script has this structure:
main.py
myRequest.py
external/
requests/
__init__.py
many files of the library...
When I run python3 main.py the file myRequest.py is imported. Inside that file, I do import external.requests as reqs.
This works for me, and also passes on Travis CI
However, when I put the above files in the folder test of my project, the Travis CI job cannot find the module:
ImportError: No module named external.requests.
When I tried running the script in an online IDE (c9, Ubuntu 14.04, Python 3.4.0) it was able to import it.
At c9, I have tried doing from .external import requests as reqs but it raises a :
SystemError: Parent module '' not loaded, cannot perform relative import.
Adding an empty __init__.py file or running python3 -m main.py did nothing.
What should I do so that the import is successful at Travis CI?
I encountered the same issue, so I am posting here in hope to help somebody:
Quick Fix
The quick fix for me was to add this line export PYTHONPATH=$PYTHONPATH:$(pwd) in the .travis.yml:
before_install:
- "pip install -U pip"
- "export PYTHONPATH=$PYTHONPATH:$(pwd)"
Have a setup.py
Having a setup.py which should be the default option as it is the most elegant.
With that you would resolve your relative import issue, try one configured like:
from setuptools import setup, find_packages
setup(name='MyPythonProject',
version='0.0.1',
description='What it does',
author='',
author_email='',
url='',
packages=find_packages(),
)
And then add this line in .travis.yml
before_install:
- "pip install -U pip"
- "python setup.py install
Related
Since a few hours I had issues with installing dependencies via pipenv. I decided to recreate my environment again and start from scratch, using a different python version (3.8.9). I'm now unable to even install any packages at all, and retrieve the following traceback:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pipenv/vendor/requirementslib/models/requirements.py", line 18, in <module>
from pip_shims.shims import (
ImportError: cannot import name 'InstallRequirement' from 'pip_shims.shims' (unknown location)
Any ideas how to resolve this?
For whatever reason, it seems an extended command is required, like so:
python3 -m pipenv install xyz
Before I was justing using
pipenv install xyz
I am currently deploying using Python 3.9 to install the depencies and setup the azure function to also use Python 3.9
Here is the requirements file I am currently using
msrest==0.6.16
azure-core==1.6.0
azure-functions
azure-storage-blob==12.5.0
pandas
numpy
pyodbc
requests==2.23.0
snowflake-connector-python==2.4.0
azure.identity
azure.keyvault.secrets==4.1.0
azure.servicebus==0.50.3
pyarrow==3.0.0
stopit==1.1.2
The bash script to install the required dependencies during the build definition
python3.9 -m venv worker_venv
source worker_venv/bin/activate
pip3.9 install setuptools
pip3.9 install --upgrade pip
pip3.9 install -r requirements.txt
My python scripts are using the following imports
import logging
from azure.storage.blob import *
import datetime
import azure.functions as func
import json
The most helpful article I could find was
https://learn.microsoft.com/en-us/azure/azure-functions/recover-python-functions?tabs=coretools
As a work-around I tried the remote build option using command:
func azure functionapp publish . Interestingly enough when I use that command the error disappears during execution and the function works as expected. I would like to enable the automatic build and deploy process again which did work until I needed to include the pyarrow library.
Any suggestions on what I am doing incorrectly?
I was able to download the content which was generated by the remote build. I then discovered it has a .python_packages folder. I now updated my install dependencies bash script to the example below which mimics how the remote build creates the the .python_packages .In essence I am copying the downloaded packages from worker_venv/lib64/python3.9/site-packages to .python_packages/lib/site-packages. My function is now executing without any errors anymore.
python3.9 -m venv worker_venv
source worker_venv/bin/activate
pip3.9 install setuptools
pip3.9 install --upgrade pip
pip3.9 install -r requirements.txt
mkdir .python_packages
cd .python_packages
mkdir lib
cd lib
mv ../../worker_venv/lib64/python3.9/site-packages .
I am trying to create a small lib package. I use Python 3.6.7 on Windows and Linux.
This is my directory structure:
my_lib\
setup.py
README.md
my_lib\
libname.py
__init__.py
tests\
test.py
For wheel creation I use: python setup.py bdist_wheel
From another machine I do: (venv) pip install my_lib.whl
But when I try to import the module it says: ModuleNotFoundError: No module named 'my_lib.libname'
When I do 'pip list' there is a package 'my-lib'
When I run python help('modules') there is a module my_lib
Is there any way to resolve this error without fixing sys.path?
Actually global pip uninstall pytest and then (venv)pip install pytest helped locally. Still need a solution for remotes
I'd like to make a wheel binary distribution, intstall it and then import it in python. My steps are
I first create the wheel: python ./my_package/setup.py bdist_wheel
I install the wheel: pip install ./dist/*.whl
I try to import the package: python -c"import my_package"
This leads to the error:
ImportError: No module named 'my_package'
Also, when I do pip list, the my_package is listed.
However, when I run which my_packge, nothing is shown.
When I run pip install ./my_package/ everything works as expected.
How would I correctly build and install a wheel?
python version 3.5
pip version 10.1
wheel version 0.31.1
UPDATE:
When I look at the files inside my_package-1.0.0.dist-info, there is an unexpected entry in top_level.txt. It is the name of the folder where I ran
python ./my_package/setup.py bdist_wheel in. I believe my setup.py is broken.
UPDATE WITH REGARDS TO ACCEPTED ANSWER:
I accepted the answer below. Yet, I think it is better to simply cd into the package directory. Changing to a different directory as suggested below leads to unexpected behavior when using the -d flag, i.e. the target directory where to save the wheel. This would be relative to the directory specified in the setup.py file.
I had the very same error, but it was due to my setup.py not specifying the entry "packages=setuptools.find_packages()".
Everythings builds nicely without that but you can't import anything even though pip shows it to be installed.
If you need to execute the setup script from another directory, ensure you are entering the project dir in the script.
from setuptools import setup
root = os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))
os.chdir(root)
# or using pathlib (Python>=3.4):
import patlib
root = pathlib.Path(__file__).parent
os.chdir(str(root))
setup(...)
In my case, in order to solve it I just had to upgrade pip (since Docker installed pip 9).
python3 -m pip install --upgrade pip
I have experienced the same situation, maybe not for the same reason, here just for reference.
The package name should not contain the dash "-", there's no error pop out, but after installing your wheel, though it is shown in pip list, you can't find that package.
/src/your-package-name # should not
/src/your_package_name # should like this
In the setup.py, you can use the name with dash "-" without limitation:
setuptools.setup(
name="instrument-lab",
...
I am running Python3.5 on Ubuntu via SSH and I have some errors there. I don't get why. If I run the following commands I get the respective errors:
(venv) root#servername: python3 __init__.py
File "__init__.py", line 1, in <module> import flask
ImportError: No module named 'flask'
If I run it with sudo like this, I get another Error:
(venv) root#servername: sudo python3 __init__.py
File "__init__.py", line 2, in <module> from .content_management import Content
SystemError: Parent module '' not loaded, cannot perform relative import
AND if I run it with Firefox, cause its a Flask App, the website works and shows NO ERRORS! Whats going on here??? I am going crazy with this!!
Seems that you didn't install Flask module on the machine where you run your python script. That produces the ImportError you get.
Install the Flask module e.g. using pip:
$ pip install Flask
After you've done so, Python should be able to load the module.