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
Related
I am building a web scraper and I am trying to import the 'requests' package but I am getting an error. I am being told the following:
ModuleNotFoundError: No module named 'requests'
Full Error:
(venv) USERs-MacBook-Pro:Scraper user$ /usr/local/opt/python#3.9/bin/python3.9 /Users/user/git/ML/Python/Practice/Scraper/Scraper.py
Traceback (most recent call last):
File "/Users/user/git/ML/Python/Practice/Scraper/Scraper.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
Steps I took when setting up project:
python3 -m venv project_name/venv
source project_name/venv/bin/activate
python3 -m pip install SomePackage
Confirmation package and dependences were installed:
(venv) USERs-MacBook-Pro:Scraper user$ pip list
Package Version
---------- ---------
certifi 2020.12.5
chardet 4.0.0
idna 2.10
pip 20.2.3
requests 2.25.1
setuptools 49.2.1
urllib3 1.26.2
By using the absolute path to system Python like that:
/usr/local/opt/python#3.9/bin/python3.9 /Users/user/git/ML/Python/Practice/Scraper/Scraper.py
you are using system's Python 3.9 and the packages that are installed for that one although you are in a virtual environment.
When creating a virtual environment you are creating a separate environment with the specified python version and all the packages you install with pip are installed to this environment and this python version.
You can better understand that if you run:
which python
inside your virtual environment.
This will show you the python that is used when you run python inside your venv and it's going to be different than
/usr/local/opt/python#3.9/bin/python3.9
So, by having installed requests using pip inside your environment, it is installed in the environments python which is executed when you run just python.
To sum up, to use the packages installed inside your venv with pip you should run your script (after activating the venv) with:
python /Users/user/git/ML/Python/Practice/Scraper/Scraper.py
I am using Pycharm and venv, and when I run my code from pycharm it works perfectly. But as soon as I build a .exe file I get this error.
I already tried to:
install pyinstaller in the venv and create .exe file
install google-api-python-client using command line instead of in venv and tried to compile .exe
pip install --upgrade google-api-python-client
pip install --force-reinstall google-api-python-client
None of this works and after checking 'site-packages' it does include googleapiclient. Please help.
in my Python Script there is an ERROR with the line
import win32events
I still installed the package pypiwin32 (with pip install pypiwin32) but it seems that this is nor the right package.
Does someone know what I need to install to use win32events in Python?
You should install pywin32 package (not pypiwin32)!!!
I use psycopg2 for my django app.
When i run python manage.py collectstatic i see django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2.
Then I run pip install psycopg2, but after successful installation pip freeze does not see it. I have installed libpq-devel and python-devel.
What am i doing wrong?
I am using Linux AMI from Amazon (EC2), if it is important.
UPDATE:
i tried to wget source and install it like python setup.py install. Now i can import psycopg2, but still can't do it in virtualenv.
Somehow, package was installed in directory lib64, but all other packages was installed in directory lib, so I copied psycopg2 from lib64 to lib and everything became all right.
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