Python virtual environment does not default python packages - python

I have to create virtual environment and move it to some remote server.
For this i created environment but it does not contains default python packages like json and logging.
I am getting error when importing some package:
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'json'
As you can see json is coming from default path not from virtual environment
source venv3/bin/activate
(venv3) [user1#nn1 ~]$ python
Python 3.7.4 (default, Dec 12 2019, 12:03:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> print(json.__file__)
/usr/local/lib/python3.7/json/__init__.py
>>> import influxdb
>>> print(influxdb.__file__)
/home/user1/venv3/lib/python3.7/site-packages/influxdb/__init__.py

Virtualenvs are not movable between servers. Did you move the virtualenv to a new server? If so I think the easiest for you is to recreate it on the remote server.

Virtual environments use absolute paths (retrieved from environment variables, for example); you can find more information in the documentation or PEP 405. So even if you move your venv to another directory, it's most likely not going to work anymore. Venvs are not supposed to be moved around. Instead you have to recreate your venv wherever you copied your project.
Recreating your venv can be greatly simplified if you use libraries like poetry or pipenv, or simply a requirements.txt file.

Virtualenv does have the default package. If it doesn't have that then you would get the error on the import statement itself. Not on the further commands.

Related

"ModuleNotFoundError: No module named 'spacy'" even though I installed it via pip [duplicate]

I am trying to import pyodbc module on a windows computer. It works in the terminal, but not the IDLE. The error message in IDLE is:
Traceback (most recent call last):
File "FilePath/Filename.py", line 3, in <module>
import pyodbc
ImportError: No module named pyodbc
This typically occurs when multiple versions of python are installed with different paths. You can check to see if you have multiple installations by opening up the IDLE terminal and using
import sys
sys.version
sys.path
These commands will print the system PATH and version of the current instance of python. Use this in both IDLE and the command line terminal to see where each differ. Once you know which version is the one you want then just remove the other. You could also remove all python instances and then reinstall a clean python environment but then you would have to re-install all of your modules using pip or easy_install
Open python in cmd (type python and press enter)
Import the module in cmd (type import modulename)
Type modulename.__file__
You will get the path where the module is stored
Copy the corresponding folder
In IDLE, import sys and typing sys.executable to get the paths where it looks for modules to import
Paste your module's folder in the path where IDLE looks for modules.
This method worked for me.
You can pip show after install package and know about location where package installed.
After that check in IDLE sys.path and if directory with package not in sys.path try to add it.
import sys
sys.path.append("/home/dm/.local/lib/python3.6/site-packages")
# or another folder that `pip show` about package.
this happen because of multiple python installed (32bit version, 64bit version) or 3v and 2.7v so to solve this problem you have to invoke the idle for that specific version like this
cd to the dir of the version that the import work fine in cmd in that folder type this command below
pythonw.exe Lib\idlelib\idle.pyw
this command will invoke idle for that version
and the import will work fine
Me too had the same issue while trying to import a module which was successfully imported on terminal and not able to install on IDLE.
How I fixed?
Assuming you know how to execute commands on terminal as well as inside of python interpreter
Open your Terminal & execute the below commands :
:~$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import sys
>>> sys.version
'3.6.9 (default, Jan 26 2021, 15:33:00) \n[GCC 8.4.0]'
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-
packages', '/usr/lib/python3/dist-packages']
>>>
Now import your module inside of your python3 interpreter.
>>> import nester
>>>
>>> nester.__file__
'/usr/local/lib/python3.6/dist-packages/nester.py'
>>>
Open your IDLE and run the below commands and compare them
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more
information.
>>> import sys
>>> sys.version
'3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit
(AMD64)]'
>>> sys.path
['','C:\Users\username\AppData\Local\Programs\Python\Python39\Lib\idlelib', 'C:\Users\username\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\username\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\username\AppData\Local\Programs\Python\Python39', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages']
>>> sys.executable
'C:\Users\username\AppData\Local\Programs\Python\Python39\pythonw.exe'
Now if you compare both outputs from Terminal & IDLE,
Terminal Module location is different from IDLE
I was using Ubuntu 18 terminal on windows machine
So I just copied my file to 'C' directory and ensured its file privileges. That's it.
:~$ cp -p /usr/local/lib/python3.6/dist-packages/nester.py /mnt/c/Users/username/AppData/Local/Programs/Python/Python39/Lib/
It worked!!
I Found the solution. It works for me
The problem is your installation directory does not match with the python version directory.
solution is >>>
type %localappdata% in your search bar then go to this folder.
here select the program folder. then select Programs , Python , Python version , Scripts
copy the location of the Scripts folder
open command prompt and type cd //yourpath (in my case cd C:\Users\3C HOUSE\AppData\Local\Programs\Python\Python37\Scripts)
if you wanna install numpy , now run pip install numpy
When you put your python scripts that have import pandas in the same folder as the site packages like pandas for example and use the same version of python that is used on CMD, it should help run your scripts in IDLE.
Check the path of your code, and that of the module. Copying the module to the path where code is worked for me.
'sys.executable' will give the path where code is stored.
For windows, open command prompt and enter pip show pyodbc to get the path of package and copy the path.
then open idle and run these lines
import sys
sys.path
Match the path from command prompt and the paths mentioned in the list provided by running above lines in IDLE. If the path is not mentioned then run these lines in idle
sys.path.append("Enter the copied path of package here")
After executing these lines, check again by importing the package that if it works for you.

Python import working on host but not inside a VM

Pythonproject directory structure is like
--test
--upperlevel
-- __init__.py
-- manager.py
-- UpperLevel.py
this files in turn contains
# __init__.py
msg = "YAYY printing !!!"
print msg
# UpperLevel.py
from upperlevel import msg
# manager.py
import UpperLevel
So in my local MAC book with python 2.7.10, started a python shell in test directory.
From that shell,
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import upperlevel.manager
YAYY printing !!!
>>>
it worked !!!!
However i started a virtual machine (ubuntu 14.04 and python 2.7.10) with vagrant and added same test directory to it.
so if i did the same thing
Python 2.7.10 (default, Jul 13 2017, 19:26:24)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import upperlevel.manager
YAYY printing !!!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "upperlevel/manager.py", line 1, in <module>
import UpperLevel
File "upperlevel/UpperLevel.py", line 1, in <module>
from upperlevel import msg
File "upperlevel/upperlevel.py", line 1, in <module>
from upperlevel import msg
ImportError: cannot import name msg
>>>
So my questions are
1) why it is not working in the later case, i tried the same in docker and getting the same error
2) there is no such file in my project, File "upperlevel/upperlevel.py", line 1, in
3) why it is searching for upperlevel.py instead of UpperLevel.py
FYI
It looks like if we do "import upperlevel" from UpperLevel.py it is refering back to itself instead of going to upperlevel/init.py.
UPDATE:
I understood where the problem is from.... my test directory(volume) is being shared between mac and vagrant/docker, somehow UpperLevel.pyc is being treated as upperlevel.pyc in that shared volume.
Instead of running in a shared directory i created same folders/files in /home/vagrant and it worked.
It seems you are running from a Mac environment, and it is possible that the Python default search paths are different for those builds, despite the version being similar.
Try comparing:
import sys
print(sys.path)
It is probable that the default installation search paths might differ.
You can use the environment variable $PYTHONPATH to add additional import paths, while I don't really like this method it can be sufficient in most cases.
You can also setup your package in a proper module installation path.
Finally answering my own question...the problem is mac has a case insensitive file system and when it is mounted on linux, python is trying to use ubuntu mode of module reading like in the case sensitive way on a case insensitive File system.
After a lot of research found this link for docker https://github.com/docker/for-mac/issues/320 so those when using ubuntu docker with python on a mac be careful with your naming conventions.

Importing a Python module works from command line, but not from PyCharm

My default Python binary is set to the one with the Anaconda distribution of Python. This is found at /home/karnivaurus/anaconda/bin/python, and I have made this the default by adding to my .bashrc file the following: export PATH=/home/karnivaurus/anaconda/bin:$PATH.
I also have a Python package called caffe, which is located at /home/karnivaurus/caffe/distribute/python, and I have added this to the package search path by adding to my .bashrc file the following: export PYTHONPATH=${PYTHONPATH}:/home/karnivaurus/caffe/distribute/python.
Now, I have a simple Python file, called test.py, with the following contents:
import caffe
print "Done."
If I run this by entering python test.py into the terminal, it runs fine, printing out "Done.". The problem I am having is when I run this in the PyCharm IDE. In PyCharm, I have set the interpreter to be /home/karnivaurus/anaconda/bin/python. But when I open test.py in PyCharm, and run the file in the IDE, I get the following error:
ImportError: No module named caffe
So my question is: Why can PyCharm not find the caffe module when it runs the Python script, but it can be found when I run the script from the terminal?
There are a few things that can cause this. To debug, please modify your test.py like so:
# Is it the same python interpreter?
import sys
print(sys.executable)
# Is it the same working directory?
import os
print(os.getcwd())
# Are there any discrepancies in sys.path?
# this is the list python searches, sequentially, for import locations
# some environment variables can fcuk with this list
print(sys.path)
import caffe
print "Done."
Try again in both situations to find the discrepancy in the runtime environment.
edit: there was a discrepancy in sys.path caused by PYTHONPATH environment variable. This was set in the shell via .bashrc file, but not set in PyCharm's runtime environment configuration.
For an additional option, you can use pycharm by terminal. And export the corresponding environment paths beforehand. This works for me. And I think it's better than make some changes in the code. You gonna need run the code by terminal after your debugging.
For example, in terminal type:
$ export LD_LIBRARY_PATH=~/build_master_release/lib:/usr/local/cudnn/v5/lib64:~/anaconda2/lib:$LD_LIBRARY_PATH
$ export PYTHONPATH=~/build_master_release/python:$PYTHONPATH
Then run pycharm by charm (pycharm can be soft linked by charm bash):
$ charm
Well this may be a redundant answer, however I think it's important to explicitly called out what causes this error.
It happened to me many times and I got it fixed by making sure that IDE ( pycharm or vscode or any other) is set to same working directory where the code resided.
for example : I have two files train.py and config.py in mlproject/src directory. I'm trying to run import config in train.py
**When run in /mlproject/ directory, I get error when try to import config **
(ml) dude#vscode101:~/mlproject$ python
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import config
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'config'
>>>
When run in /mlproject/src/` directory, I'm able to successfully import config
(ml) dude#vscode101:~/mlproject/src$ python
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import config
>>>

Can't find the test module in Anaconda Python

I cannot find the test module in my Anaconda's version of Python. Can anyone help me fix this. This module is used by the dpkt library that I am trying to use.
Python 2.7.8 |Anaconda 2.1.0 (x86_64)| (default, Aug 21 2014, 15:21:46)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named test
Quick Fix: You can checkout a copy of pystone.py from the cpython github repository and copy it to a test directory that is present in your PYTHONPATH. Or you would copy it to a test directory in your python project's root (ugly way).
Perhaps Anaconda Python does not ship with a copy of the test module. This is a standard part of Python 2.7. Other times, users accidentally overwrite their Python standard library's test module with something else. You can try to use the Python version that is shipped with OS X instead. If that fails as well, then try to see which test module is being loaded, and go from there.
import test
print test
Alpine Linux ships a python2-tests package.
the quickest way to fetch it if you don't have an Alpine lxc container is from a main repo here (or apk fetch python2-tests inside lxc).
the .apk can be uncompressed with an archiver to a .tar.gz & then just uncompress again.

Python: No module named contextlib?

Does anyone know where i can find this python module 'contextlib'?
root#overo:~# python
Python 2.6.6 (r266:84292, Mar 9 2011, 10:05:36)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named contextlib
I did not compile python myself personally. I'm just in this mess. It's running on an ARM based CPU so maybe some things were left out to save space.
I ran find / | grep contextlib which resulted in nothing.
Can i download this module from somewhere and just plonk it in /usr/lib/python2.6? Will that work?
I got this error in a different way.
I created a pipenv virtual environment using the 32bit version of Python 3.6.5 on Windows 10. I then realized I needed the 64bit version. Uninstalled the 32bit, installed the 64bit, and then tried to go back to my existing virtual env. The previously created env was now broken in odd ways and gave me this error.
I solved this by removing the old pipenv pipenv --rm and creating a new one with the newly installed version of python.
As others have noted, that module should be in the standard library, but if it's an embedded device, it may have been dropped to save space (if true, a foolish choice IMO, since leaving out contextlib.contextmanager robs the with statement of much of its power and convenience)
If you can name the specific device or manufacturer (or ask the vendor directly), you may be able to get a better answer.
As far as fixing it goes, grabbing http://hg.python.org/cpython/file/2.6/Lib/contextlib.py and dropping it in sys.path somewhere should do the trick (running python -m site will dump the list of directories that you can use)
It has been part of the standard library since 2.5 according to the docs. It seems a bit weird that you don't have it, it works with 2.6.6 for me (Ubuntu 10.10):
blair#blair-eeepc:~$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
>>> contextlib.__file__
'/usr/lib/python2.6/contextlib.pyc'
Somebody may have a better suggestion, but if it comes to it there is a link at the top of the documentation to the source code (which is Python, so you should be able to use it directly without any compilation or anything).
Edit: Unless, as Santiago Lezica suggested, you compiled your copy of Python manually, in which case it should be a simple matter of copying the module into the correct library path.
Edit for updated question: To the best of my knowledge, just dropping the source into a directory on the Python path should work. You could do this in the system library, but, to avoid it being deleted/replaced/otherwise borked in future updates, I'd recommend putting it in a separate directory and adding that directory to the Python path. You could put it under /usr/local, or somewhere in your home directory.
With Angsrom Linux, contextlib is included in the python-misc package. You can grab it by running:
opkg install python-misc
This won't, however, get you all expected python modules, so you may also want to install python-modules:
opkg install python-modules
I found one more occasion, which produces the same error.
I had made a virtual environment with python 3.6. After a updated my python version to 3.7 I tried to activate the old virtual environment and got this error.
The solution was to delete the old environment and recreate it with the new python version.
Check sys.path to make sure your python interpreter is looking in the right directories. It should look something like this (not necessarily identical):
>>> import sys
>>> sys.path
['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/pymodules/python2.6']
EDIT: With the updated information in the question that this is an install of unknown origin on a constrained device, assuming that unnecessary modules were removed to save space makes sense. However, for the record, I'll mention another, perhaps more common scenario where modules cannot be found: when there are file permissions issues. For example:
$ python -c 'import contextlib; print(contextlib.__file__)'
/usr/lib/python2.6/contextlib.pyc
$ ls -l /usr/lib/python2.6/contextlib.py*
-rw-r--r-- 1 root root 4136 Dec 26 16:42 /usr/lib/python2.6/contextlib.py
-rw-r--r-- 1 root root 4127 Jan 1 21:45 /usr/lib/python2.6/contextlib.pyc
$ sudo chmod go-r /usr/lib/python2.6/contextlib.py*
$ python -c 'import contextlib; print(contextlib.__file__)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named contextlib
Especially with custom installations, import problems due to file permission issues and path problems are some of the easiest things to check and, usually, to fix.
Python 2
sudo apt-get install python-contextlib2
Python 3
sudo apt-get install python3-contextlib2
contextlib was introduced in Python 2.5, can you remove and re-install your Python 2.6.6 again? From my copy of Python 2.6.6:
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
>>>

Categories

Resources