ImportError with VirtualEnv - python

I'm trying to use VirtualEnv for a Flask app that I'm creating since everyone has recommended me to do so. After creating my virtual environment, I installed the libraries that I needed using pip while the environment was activated. I'm running into ImportError problems with this script. The code works fine when I'm not in the virtual environment.
My script:
#!/usr/bin/python
import sc2reader
...
...
When I try to run it I get this:
(flaskapp)xxxx#xxxx-VirtualBox:~/flaskapp/bin$ ./test.py
Traceback (most recent call last):
File "./test.py", line 3, in <module>
import sc2reader
ImportError: No module named sc2reader
I've tried changing the shebang to reflect my VirtualEnv path for Python, but that didn't fix anything. The library is found in my site-packages folder in my virtual environment, so I'm not sure why I'm getting the ImportError.
I've never used VirtualEnv before so I'm assuming I configured it wrong so it's not seeing my site-packages.

Try using
#!/usr/bin/env python
as the shebang.
If that does not work, try seeing what is the output of which python.

Related

VS Code is not recognizing 'nmap'

I am trying to make a nmap scanner for the InfoSec Certification on freeCodeCamp.org and cannot get Visual Studio Code to recognize that I have installed nmap. I am very beginner and in the process of learning.
from cProfile import run
import nmap
scanner = nmap.PortScanner()
print("Welcome, this is a simple automattion tool")
print("<------------------------------------------->")
When I run this in VS Code I get the following in the terminal:
PS C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1> python3 scanner.py
Traceback (most recent call last):
File "C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1\scanner.py", line 2, in <module>
import nmap
ModuleNotFoundError: No module named 'nmap'
PS C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1>
I have so far:
Updated to the current Python 3.10.7
Installed Nmap the first time from https://nmap.org/ for Windows
Uninstalled Nmap
Reinsalled Nmap using >>>pip3 install python-nmap
For future searchers (as in my comment above), if you installed a module with pip3 but are still getting module import errors, try python3 -m pip install <module-name>.
Not sure how/why this happens (pip3 installing somewhere that python3 is not looking for modules - maybe PYTHONPATH-related), but the above can [usually] help.
It seems that Python cannot find nmap module.
You can either add it to path in Windows via Start -> Edit system environment variables -> Environment variables... and then edit PATH VARIABLE by adding at the end path to nmap module that you have installed or move the module to default Python modules directory which should be C:\Users\YourLogin\AppData\Local\Programs\Python\Python310\Lib\site-packages

Tensorflow import in VSCode fails

I'm having trouble to import TensorFlow in my python application, BUT only
if I'm running my application in VSCode (via the debugger) OR
if I'm running it from the Terminal WITHIN VSCode.
Everything is working fine if I'm running the application from the Terminal outside of VSCode.
I'm running VSCode on macOS Big Sur Version 11.1 (M1 chipset).
I have python 3.8.2 and TensorFlow installed in a virtual environment.
Here's the steps to reproduce the Error. From the Terminal outside of VSCode I run
source env/bin/activate to activate the virtual environment
python to launch python. Output to the terminal (as expected): Python 3.8.2 (default, Nov 4 2020, 21:23:28) [...]
import tensorflow as tf
print(tf.__version__) This prints "2.4.0-rc0" to the terminal (as expected).
Now, if I repeat the exact same steps 1 and 2 in the built in VSCode terminal, I get the exact same output in 2. However, if I run command 3 and try to import tensorflow, the following error message shows up:
Traceback (most recent call last):
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found. Did find:
/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/__init__.py", line 39, in <module>
from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 83, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/pywrap_tensorflow.py", line 64, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: dlopen(/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so, 6): no suitable image found. Did find:
/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
/Volumes/SSD/Jan/Documents/Github/TradingBot/env/lib/python3.8/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so: mach-o, but wrong architecture
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/errors
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
It seems like the Terminal within VSCode is not using the same site-packages as the Terminal outside of VSCode, however, running print(sys.path) gives the same result.
The same problem happens if I'm trying to run my application within VSCode whereas it is working if I'm running it from the Terminal.
Any advice is greatly appreciated.
Try Python 3.7. Many people have complained tensorflow not working on 3.8, but working on 3.7. Also, try downloading it from the google tensorflow web page.
Piggy-backing off of Jay Mody in the comments above. I would first check that the shells you use in VS code is the same as the shell you are using in the non-VS code terminal.
Try the following shell commands:
echo $SHELL
If you get the same output, then I would suggest making sure the exact same python executables are being used. Try typing this in both of the shells:
which python
In VS Code, open an integrated Terminal, after activating the environment, run
pip show tensorflow
to check if the module exists in the current environment. If not, reinstall it.
I still don't know why this problem existed in the first place but I now have it resolved by installing python 3.8 via the ARM build of mini-conda.
Here are the steps.
Download mini-conda from here https://conda-forge.org/blog/posts/2020-10-29-macos-arm64/ and install it.
After installation, create a new Conda environment conda create --name python38 python=3.8. This will install the ARM version of python 3.8
Activate the new environment conda activate python38
Create a new virtual environment for your project. python -m venv myEnv
Download and unpack the ARM version of tensorflow https://github.com/apple/tensorflow_macos/releases
Run the included script to install tensorflow. /Volumes/SSD/Jan/Downloads/tensorflow_macos/install_venv.sh --prompt and point it to your newly created virtual environment of step 4.
With these steps, tensorflow could be correctly imported.
Some more reference which pointed me to this solution:
https://github.com/apple/tensorflow_macos/issues/8
https://github.com/apple/tensorflow_macos/issues/3

Struggling to get Django server to run in virtual environment

I'm new to using Django & Python and am having some difficulty trying to get my manage.py application to run in a virtual environment.
I am receiving the following errors at the top of my models.py file:
I initially thought that this was an issue with pylint not pointing at the right environment (when I switch my Python interpretter to the one outside of the virtual environment, the pylint errors disappear); however, whenever I try to run my server, I get the following error:
(.venv) tjmcerlean#LAPTOP-RKLCBQLO:/mnt/c/Users/tjmce/Desktop/Git/CS50w/Project 2/commerce$ python manage.py runserverTraceback (most recent call last):
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
File "/mnt/c/Users/tjmce/Desktop/Git/CS50w/Project 2/commerce/.venv/lib/python3.8/site-packages/django/__init__.py", line 1, in <module>
from django.utils.version import get_version
ModuleNotFoundError: No module named 'django.utils'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 12, in main
raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
This error appears to suggest that the virtual environment is not activated. However, I activated it using the location of the virtual environment (.venv/bin/activate) and saw that the bash terminal now displays (.venv):
Additionally, when using the (pip freeze) command, I can see that Django is installed within the virtual environment too:
I'm lost as to why the server won't run and would really appreciate any advice.
Thank you!
Calling django.__file__ in activated env reveals the real path:
python -c "import django; print(django.__file__)"
it should looks like /usr/local/lib/python3.8/site-packages/django/__init__.py
then re-check PYTHONPATH environment variable.
Maybe you've installed 2 Django in venv and system-wide but in some of them there is no db. And also remove all of *.pyc files in your working folder.
The problems occur when you don't include a path in virtualenv.
Try adding the path of python on your vscode.
Or, try to install and uninstall the Django projects.

codeanywhere.com Python doesn't see virtualenv

I use codeanywhere.com try to create application with Python werkzeug and jinja2.
I install those library utilize virtualenv. Now I face the problem
Traceback (most recent call last):
File "manage.py", line 4, in
from werkzeug import Request
ImportError: No module named 'werkzeug'
virtualenv activated but nothing is works.
Does anybody has experience with codeanywhere.com?
Oh I solve this problem. In the container config file add instruction "source venv3/bin/activate" as shown on a picture add path to python as well.
enter image description here

Why do I receive an ImportError when running one of the CherryPy tutorials

I have installed CherryPy 3.1.0,. Here is what happens when I try to run tutorial 9:
$ cd /Library/Python/2.5/site-packages/cherrypy/tutorial/
$ python tut09_files.py
Traceback (most recent call last):
File "tut09_files.py", line 48, in <module>
from cherrypy.lib import static
ImportError: cannot import name static
The previous line in the file:
import cherrypy
passes without error, so it appears that it can find cherrypy on the path. What am I missing?
This works for me, and I'm also using CherryPy 3.1.0, so I'm not sure what to tell you.
Look in your /Library/Python/2.5/site-packages/cherrypy/lib directory for a file named static.py; if this file exists then I'm not sure what to tell you. If it doesn't then something has happened to your CherryPy and I'd advise you to reinstall. If it does then you should check the value of sys.path to make sure it's detecting the right version of CherryPy.
You can also try running the python interpreter on the command line and then doing a from cherrypy.lib import static to see if you get the same result.
I had an old CherryPy-2.3.0-py2.5.egg file in my site-packages. After removing the old .egg I could run the tutorial.

Categories

Resources