How do I install a python module in a virtual environment? - python

I am a beginner and read somewhere that we should always create virtual environments when working with Python. Therefore, I created a virtual environment using:
python -m virtualenv headlines
It copies all files with messages like
Using base prefix 'C:\\Program Files\\Python 3.5'
New python executable in C:\Users\Babu\headlines\Scripts\python.exe
Installing setuptools, pip, wheel...
Now, I want to install a module locally in this virtual environment using the following command:
python -m pip install feedparser
I think it is being installed in the Program Files Directory in Python 3.5 folder because the console shows:
copying build\lib\feedparser.py -> c:\program files\python 3.5\Lib\site-packages
error: could not create 'c:\program files\python 3.5\Lib\site-packages\feedparser.py': Permission denied
How can I resolve that?

I assume that you have already virtual environment folder successfully created.
First of all, you should be "inside" in your virtualenv in order to use it, thus for linux environments:
~$ source ${your_venv_folder_name}/bin/activate
will cause command line look like this
(venv)~$
Or for windows environments, like this:
python -m venv ${your_venv_folder_name}
According to this manual
python 3.4
If Python 3.4 is installed it is not necessary to install virtualenv
separately. Instead it is possible to use the venv module:
python < 3.4
virtualenv can be installed using the previously installed pip:
pip.exe install virtualenv
Now I see that you haven't enough permissions to install additional modules, so try to restart cmd terminal with administrator privileges according to this manual
Now then, with venv activated in current console and have sufficient privileges, it's should be easy to install modules from pip as usual.

Related

problems comes while creating virtualenv

I have installed python 3.10.2, while creating virtualenv
I did pip install virtualenv. then I create myenv (virtualenv mypython), but I got error like this. could you please help me what do I do
the error is :
C:\Users\ARROWIN PHOTOGRAPHY\Felix\djangoProject>virtualenv mypython
'virtualenv' is not recognized as an internal or external command,
operable program or batch file.
In python3 is better to use venv instead. Here are the steps to create your environment:
In your project directory, open the terminal:
python3 -m venv venv
Then:
source venv/bin/activate
Make sure that you have set up the python PATH variables correctly and that in your path e.g. ..\AppData\Local\Programs\Python\Python39\Scripts you can locate the python executable virtualenv.exe
If you are using multiple python versions on your local machine be careful while using PIP install. It is better to use the command pip3.x install ... (x is meant to be a specific version of pip e.g. pip3.9) so you can be sure you installed the virtualenv in the correct version of Python, and afterward, you can also select a specific python version when using virtualenv by virtualenv env -p pythonxx the xx is meant as a python version.

"No Python at..." error after installing different version and changing the path variable

I use Windows 10. I was trying to pip install Vectorbt but the installation could not have been finished because there was an error when numba and llvmlite were suppose to be installed. I was using Python 3.10 and read that there is a compatibility problem with it. So I uninstalled it and installed Python 3.8.7. Afterwards I made sure that the system variable is set on the proper Python path. I closed everything and even restarted the system but in the command line or the powershell terminal in VSC I get the error "No Python at" and the directory of the previous 3.10 installation.
The variable is set corectly (was set as an admin).
Any ideas?
Maybe something with the registry?
Thanks!
I can recommend you use virtual environments to manage python versions (you'll never look back), high level steps:
cd mydir
virtualenv .venv # creates a virtual env dev (or python3 -m venv .venv)
source .venv/bin/activate # activates the virtual env
pip install -r requirements.txt # installs the required dependencies
python -m pytest # runs the unit tests
More details in the docs: https://docs.python.org/3/library/venv.html

How to create a Python virtual environment independent of OS and Python version

I am trying to create a virtual environment to run a script which requires Python 3.6. I started off with Pipenv but I am unable to create the same environment on other platforms via the Pipfile.lock or requirements.txt unless the other platform(s) has Python 3.6 installed. I have read this post but I am unsure which direction I should take to create a virtual environment which can be shared and run its own version of Python independent of operating system and version of Python installed on the other platform.
Virtual environments are not portable, they depend on the Python installation you have.
You can't share/distribute virtual environment with others, because you can't control which version of Python others are using.
If you want to distribute your code along with all dependencies including the specific version of Python interpreter, you can use PyInstaller. It is far from perfect and little bit hacky. Also it generates a package which is specific to operating system.
https://pyinstaller.readthedocs.io/en/stable/operating-mode.html
There is also a detailed step-by-step guide on how to use PyInstaller.
https://realpython.com/pyinstaller-python/
This is step-by-step how I use Python virtual environment and share it with co-workers.
To check python and virtualenv presence, run following commands:
which python3
python3 -m pip list | grep env
which virtualenv
Install a python virtual environment builder:
python3 -m pip install virtualenv
Create a virtual environment named venv inside the project's directory: virtualenv venv
To activate this environment use this command inside project's directory: source venv/bin/activate
Install python modules dependencies listed in a requirements.txt:
python3 -m pip install -r requirements.txt
You should activate virtual environment when you working with python in this directory for package installation and for running commands in the project directory. When you need to deactivate the virtual environment do it using deactivate command.
To deactivate environment simply run: deactivate

How to install virtualenv for python3?

I am creating a Django App and stuck at virtualenv installation. I have seen this and tried to follow but it resulted in this everytime I tried something. Why can't it take python 3 for installing but this works virtualenv venv but it installs for python 2. What should I do?
virtualenv -p python3 venv
Running virtualenv with interpreter /home/oroborus/anaconda3/bin/python3
Using base prefix '/home/oroborus/anaconda3'
New python executable in venv/bin/python3
Also creating executable in venv/bin/python
venv/bin/python3: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory
ERROR: The executable venv/bin/python3 is not functioning
ERROR: It thinks sys.prefix is '/home/oroborus/PycharmProjects/test-app' (should be '/home/oroborus/PycharmProjects/test-app/venv')
ERROR: virtualenv is not compatible with this system or executable
Typing locate libpython3.5
locate libpython3.5
/home/oroborus/anaconda3/envs/tensorflow/lib/libpython3.5m.so
/home/oroborus/anaconda3/envs/tensorflow/lib/libpython3.5m.so.1.0
/home/oroborus/anaconda3/envs/tensorflow/lib/python3.5/config-3.5m/libpython3.5m.a
/home/oroborus/anaconda3/lib/libpython3.5m.so
/home/oroborus/anaconda3/lib/libpython3.5m.so.1.0
/home/oroborus/anaconda3/lib/python3.5/config-3.5m/libpython3.5m.a
/home/oroborus/anaconda3/pkgs/python-3.5.2-0/lib/libpython3.5m.so
/home/oroborus/anaconda3/pkgs/python-3.5.2-0/lib/libpython3.5m.so.1.0
/home/oroborus/anaconda3/pkgs/python-3.5.2-0/lib/python3.5/config-3.5m/libpython3.5m.a
In debian like distros python3-venv is available.
Install it using apt sudo apt-get install python3 python3-venv.
Then use it like that python3 -m venv yourvenvfoldername.
EDIT:
In this case anaconda is used, which has the replacement conda for both, pip and virtualenv. There is nice command comparison table available in the docs.
Creating a virtualenv with conda can be done like that:
conda create --name $ENVIRONMENT_NAME python

python pip install not working on windows

I have python 2.7.10 installed on windows and I am trying to install Django on the commandline with the following command:
C:/users/user/myproject> python pip install django
This displays the following error:
python: can't open file 'pip' [Errno 2] No such file or directory
Python is installed in C:\Python27 and the PATH environment variable is also set to that.
Why is pip not working?
Since Python 2.7.9 pip is included when python is installed.
However the scripts subfolder of your python installation might not be added to your PATH environment variable, and hence inaccessible by just typing pip install. However as long as your python executable is on the path, you can use the python -m flag to execute the pip module as a script:
python -m pip install SomePackage
This should work from the command line as long as python is on PATH.
If you would like to use pip directly from the cmd.exe prompt you need to add the scripts directory to your PATH environment variable:
SET PATH=%PATH%;C:\Python27\scripts
Some times in windows it especially needs Microsoft visual c++ compiler. If such error persists you can verify the log file and If needed you can download here
http://aka.ms/vcpython27

Categories