Jyoti Arora
Jyoti Arora

Reputation: 82

Python virtual environment does not default python packages

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

Upvotes: 0

Views: 1113

Answers (3)

natka_m
natka_m

Reputation: 1612

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.

Upvotes: 1

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.

Upvotes: 2

Priyanka Jain
Priyanka Jain

Reputation: 471

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.

Upvotes: 0

Related Questions