Twisted not working for all users? - python

I am working on a "chat-like" server using Twisted in Python. However I am having issues. On the main admin account of my Mac, Twisted works fine. But when I go onto a separate admin account, I get thrown...
Traceback (most recent call last):
File "/Users/Alec/Desktop/server.py", line 1, in <module>
from twisted.internet.protocol import Protocol, Factory
ImportError: No module named twisted.internet.protocol
It works fine on the other account, but this new admin account doesn't work. I am working hard to get my project going, but this has me at a halt.
Thanks!

I can only guess at what's wrong with your setup, since you haven't provided much information. However, the cause must be something to do with the way you're invoking python, since the default python on OS X has Twisted installed (as you may have noticed).
Does your administrative user have a self-compiled Python?
Is your administrative user using virtualenv, or any other environment-management tool that would remove site-packages?
Did your administrative user install Python from python.org? (This is not completely compatible with the built-in system Python and will have different packages available.)
If type -p python in a shell doesn't print /usr/bin/python, then one of these is likely the case, but fixing it (in other words, un-doing whatever has been done) depends on which of these options has altered your default Python.

You might want to check if sys.path (the module search path) is the same for both users.
>>> import sys
>>> sys.path
There may be a user specific PYTHONPATH environment variable which alters the behavior for one of the two users, although I'm not sure it could actually break the import.

Related

Pandas Installed For All Python Versions But Module Can't Be Found

I am trying to modify an AI for a game on the steam store. The AI communicates through the game with the use of a mod called the communication mod. The AI is made using a python project. The package I am trying to modify is https://github.com/ForgottenArbiter/spirecomm and the mod is https://github.com/ForgottenArbiter/CommunicationMod.
I want to add the pandas package and the job lib package as imports so I can use a model I have made for the AI. When I try to run the game + mod after adding the pandas and joblib packages as imports I get this error in the error log.
Traceback (most recent call last):
File "/Users/ross/downloads/spirecomm-master/main.py", line 6, in <module>
from spirecomm.ai.agent import SimpleAgent
File "/Users/ross/Downloads/spirecomm-master/spirecomm/ai/agent.py", line 10, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
This issue only happens when the game is running and the mod tries to run. if I just run the file in terminal it is able to compile/run and send the ready signal
I have checked that I have these modules installed and it is installed. I am on an M1 Mac and I have several different versions of python installed but I have checked them all and it is installed for each of them. I have also opened the python package using pycharm and added pandas and joblib to the python interpreter as a package.
Another thing I have tried is modifying the setup.py file to say that pandas and joblib are required. I then ran it again but I am not sure if this had any effect because I have already run it before.
There is limited help that can be provided without knowing the framework that you are is using but hopefully this will give you some starting points to help.
If you are getting a "No module named 'pandas'" error, it is because you have imported pandas in your code but your python interpreter cannot find it. There are two major reasons this will happen, either it is not installed (which you say has definitely happened) or it is not in the location the interpreter expects (most likely).
The first thing you can do is make sure the pandas install is in the PYTHONPATH. To do this look at Permanently add a directory to PYTHONPATH?.
Secondly, you say you have several versions of python and have installed the module for all versions but you most likely have several instances of at least one version. Many IDEs, such as PyCharm, create a virtual environment when you create a new project and place in it a new instance of python interpreter, or more accurately a link to one version of python. Within this virtual environment, the IDE then loads the modules it has been told to load and thus makes them available for import to the code using that particular environment.
In your case I suspect you may be using a virtual environment that has not had pandas loaded into it. You need to investigate your IDEs documentation if you do not know how to load it. Alternatively you can instruct the IDE to use a different virtual environment (one that does have pandas loaded) - again search documentation for how to do this.
Finally, if all else fails, you can specifically tell your code where to look for the module using the sys.path.append command in your code.
import sys
sys.path.append('/your/pandas/module/path')
import pandas

Can't import custom modules in python3.9 when running in wsl2

So I am trying to write some python code that will do two things, that seem to be mutually exclusive on my machine. My PC's host operating system is windows and I run Kali-Linux in WSL2 when I need to test my code on Linux. My code's main function creates two separate multiprocessing.Process objects, assigning a different thread, starting them both one after the other and then calling for them both to be joined. The plan is to allow each to run a simple server application simultaneously on different ports. This does not work when running python3 in PowerShell, as it seems to require access to os.fork() which doesn't work in said environment. When I found this out I pivoted to running in WSL2 which worked fantastically, for a time. After a while of experimenting with some ideas I decided to take some of my code and spin it off into its own file, which I placed in its own 'Libs' folder. WSL2 however, was unable to import this new file, instead giving me the exception ModuleNotFoundError: No module named 'NetStuff'. I originally had added:
sys.path.append('./Libs')
as has worked for me in the past, however when I found that WSL2 was unable to find my module, I printed out sys.path and it revealed that rather than appending my $current_working_directory/Libs like I intended, I was just appending the literal string, which wasn't useful. I then decided to try:
sys.path.append(str(pathlib.Path().resolve()) + '/Libs')
which at the bare minimum shows up as I would expect in sys.path. This, however still didn't work, python was unable to find my module and would unceremoniously crash every time. This led me to try something else, I ran my code in python3 under PowerShell again, which had no issue importing my module, it did still crash due to lacking os.fork() but the import gave no issues. Confused and annoyed I opened my code in IDLE 3.9 which, for some inexplicable reason, was able to import the file, and seemingly use os.fork(). The only major issue with running in IDLE is that it is seemingly incapable of understanding ascii colour escape characters. Given that the goal is to run my code in bash, and ideally also PowerShell, I am not satisfied with this as a solution. I returned to trying to fix the issue in WSL2 by adding my module to /home/Noah/bin, and appending this directory to sys.path, but this has still not so much as given me a new symptom.
I am utterly at a loss at this point. none of the fixes I know off hand are working, and neither are the new ones I've found online. I can't tell if I'm just missing something fundamental about python or if I'm running into a bug, if it's the latter, i can't seem to find other people with the same issue. As a result of my confusion and frustration I am appealing to you, kind users of stackoverflow.
The following is the snippet that is causing me problems in WSL2:
path0 = ('/home/Noah/bin')
path1 = (str(pathlib.Path().resolve()) + '/Libs')
sys.path.append(path0)
sys.path.append(path1)
print(sys.path)
import NetStuff
The following is output of print(sys.path) in WSL2:
['/mnt/c/Users/Noah/VSCodeRepos/Python/BlackPack', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/home/noah/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages', '/home/Noah/bin', '/mnt/c/Users/Noah/VSCodeRepos/Python/BlackPack/Libs']
The following is the error being thrown by WSL2:
Traceback (most recent call last):
File "/mnt/c/Users/Noah/VSCodeRepos/Python/BlackPack/BlackPackServer.py", line 21, in <module>
import NetStuff
ModuleNotFoundError: No module named 'NetStuff'
I am specifically hoping to fix the issue with WSL2 at the moment as I am fairly certain that getting the code to run on PowerShell is merely going to require rewriting my code so that it doesn't rely on os.fork(). Thank you for reading my problem, and if I left out any information that you would like to see just tell me and I'll add it in an edit!
Edit: I instantly realized that I should specify that my host machine is running windows 10.

How is this kind of import considered circular (or why am I getting "ImportError: cannot import name EmailMessage" error)

UPDATE:
The error (in this particular case) was not caused by circular import, but by flaw in virtualenv configuration. See my answer below, for elaboration.
I am using:
Flask 0.12.2
Python 3.6.1
I am building a web app using Flask, among other things I need an ability to send mails to users. I have built a separate Python module, which will be responsible for mail handling. Though I've encountered a strange (as it seems to me, at least) import issue, after I added the email-processing module to my app.
Here is the (insulated) import issue I've encountered:
app.py
from flask import Flask
from test_mail import EmailTool
app = Flask(__name__)
#app.route('/')
def index():
return 'Testing!'
test_mail.py
from email.message import EmailMessage
class EmailTool(object):
pass
After launching my app and going to index (i.e. /) I am receiving:
Traceback (most recent call last):
File "/app.py", line 2, in <module>
from tmp_test_mail import EmailTool
File "/test_mail.py", line 1, in <module>
from email.message import EmailMessage
ImportError: cannot import name EmailMessage
I've changed code for test_mail.py, in order to make sure email module is accessible:
import email
class EmailTool(object):
pass
This way I do not get an error.
Searching for possible causes&solutions led me to believe (1, 2, 3, 4, 5), it most likely has something to do with circular reference. Though even after reading through all the mentioned materials and insulating the problem cause, I still can not see how is it a circular reference. So I am concluding that either it is not circular and the cause lies in something else, or it is circular and I am missing something obvious here.
I am asking for help in understanding the following:
Is the case presented above considered to be a circular reference? (If yes, in what way it is actually circular)?
Why am I getting an error when I do from email.message import EmailMessage but don't get an error if I do import email instead?
To answer your both of your questions:
First
No, the code provided above shouldn't be circular, unless email.message contains references to the app module, assuming app is a valid module.
Second
Importing from email instead of email.message doesn't cause any errors because you are not importing the (seemingly) problematic EmailMessage class since it resides in email.message, not email. My theory is that it's caused by some import looping back to the app module located in the email.message module.
Note:
At the time of writing I was not aware that email or email.message were part of the standard (3.6.x) library, as I have never used anything related to those modules (and I don't use python 3.x< very often) and thus I assumed this was caused by circular reference as the author suggested. As it turns out (and pointed out in the original post) this was caused by something else entirely.
TL;DR: the actual issue had nothing to do with circular dependency — it turned out to be misconfiguration of my virtual environment (Python version was actually 2.7.10, as was suggested in the comment by hjpotter92).
How I found out it was the cause (posting as a collection of recipes for debugging your Python venv, which might later be handy for myself, and hopefully someone else):
Right after reading the comments to my quertion (hjpotter92's comment, in particular) I've rushed to check the version of Python inside the virtualenv I run my app from (even though I've checked it right before posting the question — you can't be too careful in this kind of things =).
Running (inside the virtual environment):
python --version
gave me (just as I have expected):
Python 3.6.1
I wasn't convinced, though =). As was suggested by the this answers: 1 and 2, I have added the following to code to both modules: app.py and test_mail.py (in order to check which version of Python they are actually ran by):
import sys
...
print(sys.version)
it was outputting (surprisingly for me):
2.7.10
Ok, clearly something is wrong here. I have decided to refresh my basic knowledge of virtualenv setup. The first article I stumbled upon, suggested pip --version as a second step of the setup process (after python --version). Having nothing to loose I ran it (inside the virtual environment, of course), and (to my amazement) it gave me:
pip 9.0.1 from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (python 2.7)
So, somehow pip used inside the virtual environment was a system level one...
At this point I was in doubt that my app was running from virtualenv at all. Following the recipe from this answer (and comment to it), I have composed and added to app.py and test_mail.py, the following snippet:
import sys
...
if hasattr(sys, 'real_prefix'):
print('Python 2 venv')
elif (hasattr(sys, 'base_prefix') and sys.prefix != sys.base_prefix):
print('Python 3 venv')
else:
print('Not venv!')
not much a surprise (at this point) it was printing Not venv!.
To debug what exactly is wrong with current virtual environment seemed to time-costly. So, what I ended up doing:
removing Flask and related packages (itsdangerous, Jinja2, MarkupSafe, Werkzeug) from system level (which were installed there, for some reason) via pip uninstall <package_name>.
recreating virtual environment with python3 -m venv <env_name>
Now, to answer my own questions:
Code in my question does not contain a circular reference.
The error after trying to do from email.message import EmailMessage, occurred because Python 2.7.10 was actually used, and if we'll look into source code of message.py for this version of Python, we will see that it simply does not contain a class named EmailMessage. While the source code of message.py for Python 3.6.* library, does contain class named EmailMessage.

Why doesn't import work for me? - Python

Whenever I try to import a file into python, it comes up with this error(or similar):
Traceback (most recent call last):
File "C:/Python33/My Files/username save.py", line 1, in <module>
import keyring.py
ImportError: No module named 'keyring'
I am trying to create a password storing program, and I was looking up for good ways to keep passwords secure, and someone said use import keyring, so I did, except, it never works. I must be doing something wrong, but whenever I look anything up for python, it never works out for me. It's almost as if loads have things have been changed over the years.
and idea's?
The keyring module is not part of the Python standard library. You need to install it first. Installation instructions are included.
Once installed, use import keyring, not import keyring.py; the latter means import the py module from the keyring package. Python imports should use just the name of the module, so not the filename with extension. Python can import code from more than just .py python files.
I was getting the same error "ModuleNotFoundError: No module named 'keyring'". And after installing this module pip install keyring, the same error occured with another module name. Then I came to the conclusion that it is the fact that the VSCode is not able to connect to my venv, even after setting the Python Itereptor. Press CTRL + SHIFT + P, and then type Python: Select Interceptor, and select your venv, if you want to set this.
To fix the issue, I had to force the VSCode to use the .venv I created, and luckily there is a dropdown to do that, on the top right corner as in the preceeding image. Click ont the Python version, and then you will be able to select your virtual environment.
Now it will take the modules from your virtual environment.

Is there any way to get python omnicomplete to work with non-system modules in vim?

The only thing I can get python omnicomplete to work with are system modules. I get nothing for help with modules in my site-packages or modules that I'm currently working on.
Once I generated ctags for one of my site-packages, it started working for that package -- so I'm guessing that the omnicomplete function depends on ctags for non-sys modules.
EDIT: Not true at all.
Here's the problem -- poor testing on my part -- omnicomplete WAS working for parts of my project, just not most of it.
The issue was that I'm working on a django project, and in order to import django.db, you need to have an environment variable set. Since I couldn't import django.db, any class that inherited from django.db, or any module that imported a class that inherited from django.db wouldn't complete.
I get completion for my own modules in my PYTHONPATH or site-packages. I'm not sure what version of the pythoncomplete.vim script you're using, but you may want to make sure it's the latest.
EDIT: Here's some examples of what I'm seeing on my system...
This file (mymodule.py), I puth in a directory in PYTHONPATH, and then in site-packages. Both times I was able to get the screenshot below.
myvar = 'test'
def myfunction(foo='test'):
pass
class MyClass(object):
pass
Just ran across this on Python reddit tonight: PySmell. Looks like what you're looking for.
PySmell is a python IDE completion helper.
It tries to statically analyze Python source code, without executing it, and generates information about a project’s structure that IDE tools can use.
While it's important to note that you must properly set your PYTHONPATH environmental variable, per the the previous answer, there is a notable bug in Vim which prevents omnicompletion from working when an import fails. As of Vim 7.2.79, this bug hasn't been fixed.
Trouble-shooting tip: verify that the module you are trying to omni-complete can be imported by VIM. I had some syntactically correct Python that VIM didn't like:
:python import {module-name}
Traceback (most recent call last):
File "<string>", line 1, in ?
File "modulename/__init__.py", line 9
class empty_paranthesis():
^
SyntaxError: invalid syntax
Case-in-point, removing the parenthesis from my class definition allowed VIM to import the module, and subsequently OmniComplete on that module started to work.
I think your after the pydiction script. It lets you add your own stuff and site-packages to omni complete.
While your at it, add the following to your python.vim file...
set iskeyword+=.
This will let you auto-complete package functions e.g. if you enter...
os.path.
and then [CTRL][N], you'll get a list of the functions for os.path.

Categories