How to specify __init__.py while building a python library - python-3.x

This is the first python library I am building. The folder structure looks like:
mylibrary
- build
- data
- docs
- environment.yml
- README.md
- license
- setup.py
- my library
- __init__.py
- Module1
- __init__.py
- module1_worklfow.py
- Module 2
- __init__.py
- module2_worklfow.py
On the init.py under "mylibrary" I have the statement from .Module1 import classfrommodule1workflow and from .Module2 import classfrommodule2workflow.
On the __init__ files that are inside the modules, I have from mylibrary.module1.module1_workflow import classfrommodule1workflow and the same for module2 init.
Now if I install the library through the conda command line with wheel, the library imports well. I am able to run in python from mylibrary import module1 but then if in the same command line I open a jupyter notebook, I am not able to import the library within jupyter. It says - no module named mylibrary.module1
I'm almost sure there's something wrong with my main init file, just can't figure out what it is.
EDIT: folder with files for MCVE here.
Steps on anaconda prompt:
cd folder_path
conda env create -f environment.yml
conda activate mylibrary
python setup.py bdist_wheel
pip install path_to_wheel
python
import mylibrary
import mylibrary.module1
quit()
This won't throw any errors, which is great! Then on the same command line:
jupyter notebook
then on the jupyter notebook:
import mylibrary
yields:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-8c582ad816fa> in <module>
----> 1 import mylibrary
ModuleNotFoundError: No module named 'mylibrary'
and
import mylibrary.module1
yields:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-64d831ac2965> in <module>
----> 1 import mylibrary.module1
ModuleNotFoundError: No module named 'mylibrary'
Here's an image for clarity:

Edit:
OK, I downloaded your code and it seems to be an issue with your setup.py after all. You forgot to add packages=find_packages(), to the argument list of setup, so the package you were installing was effectively empty. When import mylibrary was working for you when launching python from command line, it was probably because import also loads modules from current directory (try launching python from a different directory and check if it works).
By the way, you don't have to build a wheel if you just want to install from source. You can use pip install . or python setup.py install. Alternatively, for development purposes it's useful to do pip install --editable . or python setup.py develop.
Original answer:
Sounds like your package/library is OK but when you're launching jupyter notebook it's probably using a different interpreter. It should help to install jupyter in the conda environment. If it still doesn't work, you can try launching it with python -m jupyter notebook to ensure that you're using the same interpreter as for installing your package.

Related

Packages doesn't set up on python venv

I'm trying to build pyslam project, and the install.sh file was provided. But when I executed install.sh, the main.py didn't work.
https://github.com/luigifreda/pyslam#install-pyslam-under-ubuntu-2004
Full Installation
In order to run main_slam.py with venv, get in the root of this repository and run the following command:
$ . install_all_venv.sh
N.B.: do not forget the dot! (without '/' !)
This will compile the required thirdparty packages and will also activate the created pyslam environment. Now, from the same terminal, you can run:
$ python3 -O main_slam.py
The result was
(pyslam) (base) ict-526#ict526-MS-7D32:~/pyslam$ python3 -O main_slam.py
Traceback (most recent call last):
File "/home/ict-526/pyslam/main_slam.py", line 27, in <module>
from config import Config
File "/home/ict-526/pyslam/config.py", line 27, in <module>
import yaml
ModuleNotFoundError: No module named 'yaml'
I think the packages didn't install in the venv (pyslam). How can I fix this situation?
My environment is ubuntu20.04, and python 3.9.7.
According to the installaion instruction, python 3.6.9 was required. After install_all_venv.sh, the pyslam venv support python 3.9.7...

ModuleNotFoundError: No module named 'win32api

I am using embedded python 3.7.4
Getting the folliwng error when trying to create new file in jupyter notebook
File "D:\Users\sgangop7\python3.7.4\Lib\site-packages\jupyter_core\paths.py", line 387, in win32_restrict_file_to_user
import win32api
ModuleNotFoundError: No module named 'win32api'
Solutions Tried:
fresh install of jupyter notebook
pip install pypiwin32
pip install pywin32
Running the pywin32_postinstall.py
Added 'D:\Users\sgangop7\python3.7.4\Lib\site-packages' in the PATH variable of environment variable
Copied 'win32api.pyd' to 'D:\Users\sgangop7\python3.7.4\Lib\site-packages\win32\lib'
I cannot find any other solution for how to fix this issue.
On your jupyter notebook, you can try these commands :
import sys
sys.path
You will see a list of paths which Python is using to find packages. If your path "D:\Users\sgangop7\python3.7.4\Lib\site-packages\win32\lib" isn't in the list, you may need to add it. You can do it like this :
sys.path.insert("D:\Users\sgangop7\python3.7.4\Lib\site-packages\win32\lib")
Good luck !

64bit MacOs After succesfully installing Pygame: ModuleNotFoundError: No module named 'pygame'

Python noob here. I've been trying to install pygame and pandas for a few hours now. Even with Conda I did not succeed. I have Python 3.8.5 installed.
I eventually tried through the terminal with these commands:
python -m pip install pygame==2.0.0.dev6
and
python -m pip install pandas
(this was a total guess by the way, but apparently it did something)
Results were succesfull:
Requirement already satisfied: pygame==2.0.0.dev6 in /opt/miniconda3/lib/python3.8/site-packages (2.0.0.dev6)
and
Successfully installed numpy-1.19.2 pandas-1.1.2 python-dateutil-2.8.1 pytz-2020.1
But, when I try to import either modules, I still get errors. Any ideas?
import pygame
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'
import pandas
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import pandas
ModuleNotFoundError: No module named 'pandas'
Do I need to move the modules to the script folder or something? Or what do I even move? Thanks!
Ok.. Got it thanks to #matt.. after succesfully installing pygame, find the environment in which pygame is installed by entering 'which python'.
In my case it returned:
/opt/miniconda3/bin/python
Now I needed to make sure the VS console was pointing at the same environment, by checking which python interpreter it was using and selecting the correct one. More info:
how to check and change environment in VS Code

ModuleNotFoundError when run python script but in cmd prompt type 'import colorama' is works

Window10 and Python3.7.4
The lib I am trying to use is 'colorama'.
When i run the python script file (x.py) it cannot find the module in the line which is "import colorama", but when i open a cmd and enter python env then type 'import colorama', it works. Does any one know what's the reason?
'''
>>>D:\Scratch\commands>mypythonscript.py
>>>Traceback (most recent call last):
>>> File "D:\Scratch\commands\mypythonscript.py", line 12, in <module>
>>> from colorama import init, Fore, Back, Style
>>>ModuleNotFoundError: No module named 'colorama'
'''
I don't know why this issue arise but maybe I installed VS2019. Before this, it worked fine for me.
Try to find which python your are working with using SYS module with its VERSION method in this way:
import sys
print(sys.version)
And then verify if you are using the same python version as you are using on cmd.
Another problem could be the path of your module, try to locate it with the "magical method" of your module installed, for example in this case:
import colorama
print(colorama.__file__)
And then verify if colorama is in this path: ".../Python37/lib/site-packages/colorama"
if not, download COLORAMA from here:
https://www.lfd.uci.edu/~gohlke/pythonlibs/
unzip it with 7ZIP
and all unzip files copy to : ".../Python37/lib/site-packages"
don't worry for the module version, colorama is for all python versions.
NOTE: this only works for windows version.

using cx_freeze in spyder

I installed cx_Freeze using pip:
C:\Users\Sarah\Documents\PythonScripts>python -m pip install cx_Freeze --upgrade
Collecting cx_Freeze
Downloading cx_Freeze-5.0.2-cp36-cp36m-win_amd64.whl (162kB)
Installing collected packages: cx-Freeze
Successfully installed cx-Freeze-5.0.2
However, I get this error when I try to execute a script:
setup.py build
Traceback (most recent call last):
File "C:\Users\Sarah\Documents\PythonScripts\setup.py", line 9, in <module>
from cx_freeze import setup, Executable
ImportError: No module named cx_freeze
When I check the system path, I get all this information
print (sys.path)
['', 'C:\\Users\\Sarah\\Anaconda3\\lib\\site-packages\\spyder\\utils\\site', 'C:\\Users\\Sarah\\Documents\\PythonScripts', 'C:\\Users\\Sarah\\Anaconda3\\python36.zip', 'C:\\Users\\Sarah\\Anaconda3\\DLLs', 'C:\\Users\\Sarah\\Anaconda3\\lib', 'C:\\Users\\Sarah\\Anaconda3', 'C:\\Users\\Sarah\\Anaconda3\\lib\\site-packages', 'C:\\Users\\Sarah\\Anaconda3\\lib\\site-packages\\Sphinx-1.5.6-py3.6.egg', 'C:\\Users\\Sarah\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Users\\Sarah\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Users\\Sarah\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\Users\\Sarah\\Anaconda3\\lib\\site-packages\\setuptools-27.2.0-py3.6.egg', 'C:\\Users\\Sarah\\Anaconda3\\lib\\site-packages\\IPython\\extensions', 'C:\\Users\\Sarah\\.ipython']
I am assuming that somehow, the path to the cx_freeze module is lost in here somewhere. I would really appreciate some simple (novice user) ideas for how to go about fixing this.
I tried copying the folder cx-freeze to the PythonScripts folder but that didn't help.
I know why you are getting this error and I do not think it has anything to do with the file path.
Try:
from cx_Freeze import setup, Executable
in the place of
from cx_freeze import setup, Executable
in your setup.py script
and this error should fix (you had f not F).

Resources