Can not install "posix" package using pip - python

I installed Anaconda on my PC (windows), then I tried to run the following:
import pdb
import sys
import os
import posix
sys.path.append(os.path.join(posix.environ['HOME'], 'mlprojects', 'swig', 'src'))
sys.path.append(os.path.join(posix.environ['HOME'], 'mlprojects', 'datasets', 'python'))
sys.path.append(os.path.join(posix.environ['HOME'], 'mlprojects', 'mlopy', 'netlab'))
##ENDSETUP
import ndlml as nl
import ndlwrap as nw
import numpy as np
import datasets
import netlab
import matplotlib.pyplot as pp
import matplotlib.mlab as ml
import matplotlib.axes
import math
After running the above code, I got the following error:
File "C:/Users/yat/test.py", line 9, in <module>
import posix
ImportError: No module named posix
When I tried pip install posix, I got the following message:
Collecting posix
Could not find a version that satisfies the requirement posix (from versions)
No matching distribution found for posix`
How do I install posix package, and where to find all installed packages in Anaconda?

That's a built-in module that's not available on Windows. Look at the documentation:
Do not import this module directly. Instead, import the module os,
which provides a portable version of this interface. On Unix, the os
module provides a superset of the posix interface. On non-Unix operating
systems the posix module is not available, but a subset is always
available through the os interface. Once os is imported, there is
no performance penalty in using it instead of posix. In addition,
os provides some additional functionality, such as automatically calling
os.putenv when an entry in os.environ is changed.
So just replace posix with os and you should be good to go.
To list all the installed packages with Anaconda, look at the docs:
List all of your packages in the active environment:
conda list
To list all of your packages installed into a non-active environment
named snowflakes:
conda list -n snowflakes

Related

No module named 'importlib.util' with Python 3.8

I encountered the following error:
File "D:\python-win\lib\distutils\util.py", line 9, in <module>
import importlib.util
ModuleNotFoundError: No module named 'importlib.util'
I am using python 3.8 (x64) on a windows 10 machine
D:\test> python --version
Python 3.8.10
and the latest pip
(.venv) D:\test> pip --version
pip 21.3.1 from d:\test\.venv\lib\site-packages\pip (python 3.8)
As the above message indicates the error is originating from util.py which is included in the lib/dist folder of the python 3.8 distribution for windows. The offending block is
"""distutils.util
Miscellaneous utility functions -- anything that doesn't fit into
one of the other *util.py modules.
"""
import os
import re
import importlib.util
import string
import sys
from distutils.errors import DistutilsPlatformError
from distutils.dep_util import newer
from distutils.spawn import spawn
from distutils import log
from distutils.errors import DistutilsByteCompileError
All of the info I could find indicated importlib was included with python 3+, but I am seeing this error anyway.
Is there something else I am missing?

pywin32 ImportError: DLL load failed: The specified module could not be found

I'm using python 3.6 and attempting to import win32com.client as win32 to be able to send an email with outlook but keep getting the above import error.
I've installed both pywin32 and pypiwin32 and run the postinstall script (did see it say "You do not have the permissions to install COM objects.)
I see the pywintpyes36.dll and pyhoncom36.dll in the location I'm running the script out of.
I've done a bit of searching and can't seem to find what I'm missing. Is it the fact that I was unable to install the COM objects? Any help would be greatly appreciated, thanks.
[EDIT]
I'm working out of a venv, and have installed both libraries using py -m pip install pywin32 and py -m install pypiwin32. I verified that I see the installation in the "site-packages" folder within my venv.
my imports:
import sys
sys.path.append("C:\path\venv\Lib\site-packages")
import tkinter as tk
import getpass
import os.path
import time
import os
import win32com.client as win32
import sqlite3
from datetime import datetime
from functools import partial
I have the sys.path.append there otherwise the module is not found at all.
Full error message:
Traceback (most recent call last):
File "C:\path\program.py", line 8, in <module>
import win32com.client as win32
File "C:\path\venv\Lib\site-packages\win32com\__init__.py", line 5, in <module>
from win32 import win32api
ImportError: DLL load failed: The specified module could not be found.
This may not be directly relevant for the OP, but may be of help to others who end up here based on the title.
Summary
Possible workaround for conda environments with Python>=3.8:
DO NOT pip install pywin32, but
DO conda install pywin32 (e.g. from conda-forge)
Details
I encountered the following error in a freshly created (Mini-)Conda environment with Python 3.9 on windows 10, after installing pywin32 via pip:
ImportError: DLL load failed while importing win32file: The specified module could not be found.
The pywin32 installation instructions mention this type of issue explicitly, and there are several related issues.
However, in my case something else was going on.
Here are some interesting observations:
I have numerous other, pre-existing, conda environments, with python versions ranging from 2.7 to 3.8, and corresponding pywin32 versions, in which the issue does not arise (just verified this).
All these environments have their own pywintypesXX.dll etc. yet they coexist peacefully.
There is no pywintypesXX.dll in my system32 folder.
I have never needed to run the pywin32_postinstall script, yet.
As it turns out, on my system, the import error only arises if I do a pip install pywin32 in a conda environment with Python>=3.8. The issue does not arise for Python 3.7 (nor for 2.7).
In a Python>=3.8 conda environment, conda install pywin32 fixed the issue (instead of using pip).
Apparently Python 3.8 changed the way dll files are found. This change has been incorporated in pywin32, but can still cause trouble if you mix conda and pip.
Related:
DLL load failed: The specified procedure could not be found. win32api, sys, os
How to fix "ImportError: DLL load failed" while importing win32api
How to fix 'DLL load failed while importing win32api' in Jupyter notebook after connecting VS code to Jupyter notebook?

How to import r-packages in Python

I'm a bit troubled with a simple thing. I was trying to install a package called hunspell, but I discovered it is originally an R package. I installed this version: https://anaconda.org/conda-forge/r-hunspell, but I'm not being able to import it. Is this package supposed to work with Python? Should I use rpy2 to import it? First time using cross-platform packages so I'm a bit confused.
Just to be clear, import hunspell brings ModuleNotFoundError: No module named 'hunspell' and import r-hunspell brings SyntaxError: invalid syntax.
I also noticed that this package, also installed an r-base package, but I'm also not sure how to import that.
After running in the command line:
pip install rpy2
or with the "!" if you're in a Jupyter Notebook. The following procedure will answer your issue, based on the official documentation:
# Using R inside python
import rpy2
import rpy2.robjects.packages as rpackages
from rpy2.robjects.vectors import StrVector
from rpy2.robjects.packages import importr
utils = rpackages.importr('utils')
utils.chooseCRANmirror(ind=1)
# Install packages
packnames = ('hunspell', 'some other desired packages')
utils.install_packages(StrVector(packnames))
# Load packages
hunspell = importr('hunspell')
If you want to access specific functions in this module you could check out these answer or that answer too.

How to install the os module?

I'm trying to install the os Python module on Windows.
In a cmd console, I typed:
C:\Users\username> pip install os
Collecting os
Could not find a version that satisfies the requirement os (from versions: )
No matching distribution found for os
So I guessed the module was not available on Windows for some reasons, but I found references of it in some SO questions.
Obviously, typing Windows and os in Google only gives me answers about Windows itself.
So, how can I install the os module on Windows?
See Also:
Determining if a given Python module is a built-in module - to learn how to check if a library is built-in and hence doesn't require installing.
OS is python's standard library. So no need to download it.

Python script can't open zmq-module

I've a problem with a python-script. In a script, i have:
import zmq
from datetime import datetime
import pytz
import bottle
import logging
from bottle import response
But, when i run this script, i see:
ImportError: No module named zmq
I've also tried:
import pyzmq
When i run: pip install pyzmq, it display's that the package already is installed.
But it doesn't works. The probleem arose after i have updated Python 2.6.x to Python 2.7. I think, the plugins are not included in the upgrade?????
Does anyone have an idea?

Categories