Is it possible to import CPython variables without PyPy module conflicts - python-3.x

I have a script that is using CPython and has pywin32 modules win32gui, and win32con. I have a variable that I want to import from this script into a different PyPy script to use its value.
It looks something like this:
from functions import var1
however when I try to run this code I get the error
Traceback (most recent call last):
File "d:/folder/functions.py", line 5, in <module>
import win32gui, win32con
ModuleNotFoundError: No module named 'win32gui'
All I need is the value of the variable which is an integer, in any way possible. I think I could perhaps make a function that wraps the value of the variable and send it to the script that way, but I think that might be inefficient.
Is there someway, to avoid module conflicts like this when using different interpreters for different scripts?

You can do something like
try:
import win32gui, win32con
except ImportError:
win32gui = None
wn32con = None
then later in your code, where you use these imports, check if they are None first.

Related

Python Relative Imports but I copied the documentation

I read all the other SO posts about this and it either doesn't work or uses sys.path.append.
Below is a replica of the official documentation:
All other files not shown are empty
moduleA.py
from ..subB.moduleB import MyClass
moduleB.py
class MyClass:
def __init__(self):
pass
package/subB/__init__.py
from .moduleB import MyClass
Traceback from running moduleA.py
Traceback (most recent call last):
File "path\to\my\projects\folder\package\subA\moduleA.py", line 1, in <module>
from ..subB.moduleB import MyClass
ImportError: attempted relative import with no known parent package
File Structure
The Python docs unfortunately forget to mention that their example only works if you run your code in a very specific way using the '-m' switch. So you would have to do python -m subA.moduleA and you need to ensure that your current working directory is package. Otherwise it will fail.
If you don't like these restrictions (like me), I've created an experimental import library: ultraimport
It gives you more control over your imports and lets you do file system based imports.
In moduleA.py you could then write:
import ultraimport
MyClass = ultraimport('__dir__/../subB/moduleB.py', 'MyClass')
This will always work, no matter how you run your code or what is your current working directory. Also no need to change sys.path. It will actually go to the file system and load the very file you've specified.

Unittest: filename with point not loaded properly

Setup: PyCharm, Python 3.10
We have the naming convention to name our python unittest files like an URL. For example: my.domain.org.py
In the past, this was no issue. Now after an IDE and Python Update it does not run anymore. Selecting right click -> Run "Python tests in my.domain.org.py" throws the error:
Traceback (most recent call last):
File "D:\programs\Python\3.10.2\lib\unittest\loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
ModuleNotFoundError: No module named 'my'
It seems, the loader is interpreting the "." in the filename as path.
How can I run the unittest without renaming the file (which solves the issue)?
you cannot import python files with invalid names (in your case has dots in it) directly, but there's a turn around, you can use the imp library like (here in the example I have a function named print_smth that prints "it works!" in the my.file.py):
import imp
with open('my.file.py', 'rb') as fp:
my_file = imp.load_module(
'my_file', fp, 'my.file.py',
('.py', 'rb', imp.PY_SOURCE)
)
if __name__ == "__main__":
my_file.print_smth()
output:
test.py:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
it works!
P.S: preferably DO NOT DO THAT! it is highly deprecated!

Import don't run in python

I am developing a Django project and I am trying to run a test file but an error in the import occurre.
My folder hierarchy to be like the image:
see the image of function process_data inside of core.py
I am in the test directory trying to run my test file.
My import to be like the code:
from coordenadas.core import process_data
but, when I run my code an error is showed:
Traceback (most recent call last):
File "tests_class_moviment.py", line 1, in <module>
from coordenadas.core import process_data
ModuleNotFoundError: No module named 'coordenadas'
I am tryed use relative import
from .coordenadas.core import process_data
from .core import process_data
from ..coordenadas.core import process_data
from ..core import process_data
but the only way that no error is showed in pycharm is the
from coordenadas.core import process_data
Some idea how can I solve it?
On a partially unrelated note, absolute imports are definitely recommended over relative imports. PEP8:
Relative imports for intra-package imports are highly discouraged. Always use the absolute package path for all imports. Even now that PEP 328 [7] is fully implemented in Python 2.5, its style of explicit relative imports is actively discouraged; absolute imports are more portable and usually more readable.
As for the module error itself, adding an __init__.py file to the "coordenadas" directory will turn it into a module, which is exactly what you need.

Cython: 'PyxImporter' object has no attribute 'find_spec'

I'm trying to integrate a Cython module into my project and I'm having trouble getting it to compile correctly. I have traced my problem to this minimal example:
Say I have two files a.py and b.pyx located in the same directory, if I then do the following in a.py:
import pyximport; pyximport.install()
import b
Then everything works fine, b.pyx is compiled and imported successfully.
But if I instead do this in a.py, assuming that a.py and b.pyx are located in dir1/dir2:
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
import pyximport; pyximport.install()
import dir1.dir2.b
(which is somewhat nonsensical in this example but illustrates what's preventing me from importing a from elsewhere in my module hierarchy), I get the following exception:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 888, in _find_spec
AttributeError: 'PyxImporter' object has no attribute 'find_spec'
Is this expected behaviour or is something wrong with my installation? (I'm on Windows 10)
NOTE: there is another question with a very similar title on SO which does not answer my question at all.
I had this error message (though not necessarily the same path situation) and it was resolved by upgrading Cython from 0.28.5 to 0.29.

Zipline import error. No module named zipline.transforms

I am not able to import the zipline.transforms module
>>> from zipline.transforms import batch_transform
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'zipline.transforms'
Disclaimer: I'm currently a maintainer of Zipline.
I'm guessing the reason you're seeing this error is because that particular module was removed a while back (assuming you're using zipline 1.0.0 or later). If you want to do things similar to transforms you'll need to call data.history() to get your pricing data, and call numpy/pandas functions like .avg() or .std(), or use talib.
I think you should firstly print out your sys.path (print sys.path), and then see where you zipline module is installed (somewhere like .../lib/python2.7/site-packages/zipline). Usually "no module named XXX" is caused by you sys.path doesn't contain the path you installed zipline. You should just add your zipline path into sys.path. Also use anaconda is good for zipline (http://www.zipline.io/install.html), so as to keep the environment tidy and clean.

Resources