[Answered ]-How to import packages in virtualenv in python shell

2👍

I would change the shebang to use the Python from your virtual environment.

#!/home/lkm/Folder/project/app/myvenv/bin/python

Then you shouldn’t have to append the virtual env to the python path, and you can remove the following line.

sys.path.append("/home/lkm/Folder/project/app/myvenv/")

However, if you really want to manually add the virtual env directory to the Python path, then I think you want to include the site-packages directory instead:

sys.path.append("/home/lkm/Folder/project/app/myvenv/python3.4/site-packages")

0👍

How are you executing the file?
I see you have:

#!/usr/local/bin/python3.4

which means that if you’re executing the file with:

./file.py

it will be executed with the system interpreter.

You need to activate the environment:

$ source env/bin/activate

and execute the file with:

$ python file.py

FWIW, I think the cleanest solution though is to have a setup.py script for your project (packages= argument being the most important) and define an entry point, similar to:

entry_points = {
    'console_scripts': ['my-script=my_package.my_module:main'],
}

Then you run python setup.py develop after activating the environment
and you would run the script simply as a command:

$ my-script

Leave a comment