pip install on virtualenv - python

I installed VMWare on window and using a virtualenv for python project.
I activated the project project1 and pip3 install xxxxx modules I need.
But some of them had an import error and when I deactivated the virtualenv and pip3 install xxxx on the root, the import error is resolved.
Are we supposed to pip install on the root, even though we activate the virtualenv? Thanks.

# A virtualenv's python pip call:
$ project1/bin/python -m pip install xxxxx
Where:
project1: Is the env folder inside your root folder that contain all
your python modules included python it self.
-m is a python flag stand for: "run library module as a script" .
pip: Is the package manager.
install: is the pip command to install the desired module.
xxxxx: its the desired python module (no, its not about film genres)
P.S: In this way you ensure to be using the python version you installed on your virtualenv unequivocally

Related

Python not recognizing virtualenv

First I run command pip install virtualenv then after I run python -m virtualenv venv, I get this following error msg
"/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named virtualenv"
Cuurently, I'm using python v2.7.16 and when I run pip freeze | grep virtualenv , I get virtualenv==20.4.2 so virtualenv is there. When I run which python I get /usr/bin/python and I don't have .bash_profile when I run ls -a. I am using mac. What could be the reasons python not recognizing virtualenv when it's there?
You may create .bash_profile and it is auto-recognised by the macintosh machine.
Please also run which pip and make sure the pip is in the same bin as your python (/usr/bin/python)
The bottom line is pip used to install a package by default will install the packages in the bin directory that also stored your python executable.

Probability that package installed using pip install --user will be loadable

My software needs to install packages on arbitrary container images that have python 3.5+. The module must then be loadable by running python3 -c 'import my_module'.
I plan to use python3 -m pip install my-package --user command to install the packages. The --user work around the containers where the active user is not root.
Unfortunately I've vaguely heard about cases where the user directory where pip installs packages is not in PATH on some systems or something like that.
Would that affect the ability to import the installed package?
What command-line should I use to install packages reliably?
as long as it's in your path such as .local/bin or in .local or python PATH's you can run it as if it was in /usr PATH

Multiple python virtual env

Suppose i have normal system python 2.7 packages in system locations
Then i do
virtualenv env1
I install all requirements there
Then i deactivate that and do
export PYTHONPATH=$PYTHONPATH:/path/to/env1
Then i do virtualenv env2 and then install new requirements there
then i do this again
export PYTHONPATH=$PYTHONPATH:/path/to/env2
So my questions is
All the packagaes which are installed in env1 , will not be installed in env2 or env2 will install new packagaes.
I ahve problem where if i pip install packagae1 while env2 is activated. it says requirement already met
Then i open python shell and do import mymodule. It says module not found. I can see that module was there in env1.
I had to remove that module from env1 and then install on env2 and then it work.
I want to know whay is that
Have you activated env2 before installing module ?
source bin/activate
If you want to uninstall any module from virtualenv, then use
pip uninstall module_name
Firstly, do not change PYTHONPATH manually.
Steps should look something like this:
[root#demo src]$ source /usr/local/env1/bin/activate
(env1)[root#demo src]$ # pip install blah
(env1)[root#demo src]$ source /usr/local/env2/bin/activate
(env2)[root#demo src]$ #pip install blah
(env2)[root#demo src]$
Probably you have not activated the virtual environment (lets call it as venv) and installed the package system wide.
I suggest you to try activating the venv first and then proceed with installations in either of the venv.
You can activate venv using following code :
cd ~/venv/
source /bin/activate

'virtualenv' is not recognized as an internal or external command, operable program or batch file

After I installed Python and Djangom, I'm trying to use virtualenv for django project purpose using virtualenv. I installed virtualenv using pip.
pip install virtualenv # got install successfully
When I tried to run it, I got the error message
C:\Users\gshiv\Desktop\DjangoProject>virtualenv
'virtualenv' is not recognized as an internal or external command,
operable program or batch file.
steps:
- go to where you want create django app on that folder.
then run this command on command prompt : python -m virtualenv .
(eg. C:\Users\gshiv\Desktop\django>python -m virtualenv .)
where django is the my folder i want run virtualenv and .(dot) indicates virtualenv install all it's folder in django folder otherwise you can use other folder name instead .(dot) this time virtulenv creates a folder in main folder(django) .
after running this command: run .\scripts\activate now you can see this type of line on cmd-prompt (django) C:\Users\gshiv\Desktop\django>
i.e main folder name before the source path. now you can install any modules for your project that belongs to that main folder only.
pip install django works fine.
If you can not find your virtualenv command in the windows console/terminal after installing it with pip, try this to make your environment 🔽
python -m virtualenv <nameOfEnv>
If you want to use a specific version of python, initialize it like this 🔽
python -m virtualenv <nameOfEnv> -p=<C:/path/to/python/version3.x.x/python.exe>
When using windows for first installation, you can use python from WindowsApp
Run pip uninstall virtualenv and then pip install virtualenv
There are three points that you need to consider:
Make sure that in the windows PATH variable there is an entry with your python installation and the scripts subfolder eg: C:\Program Files (x86)\Python36-32\ and C:\Program Files (x86)\Python36-32\Scripts\
When using pip install virtualenv, make sure that you run cmd as administrator. Otherwise, there might an access denied error during installation and virtualenv will not be installed properly.
Make sure that virtualenv has been installed correctly. Check in the python scripts subfolder - there must exist an .exe named virtualenv.exe. If not, uninstall will pip uninstall virtualenv and install again.
When I ran the pip install virtualenv command I got:
Requirement already satisfied: virtualenv in c:\directory\to\appdata\roaming\python\python36\site-packages
so I tried forcing upgrade:
pip install --upgrade --force virtualenv
py -3 -m venv venv
try using the above command.
virtualenv venv
will work on only older version of python
Use
python -m venv abc
Where abc is the name of the virtual environment
Run CMD as administrator and then enter
pip uninstall virtualenv
then re-run CMD as administrator and run
pip install virtualenv
Step 1: Run pip uninstall virtualenv.
Step 2: Run pip install virtualenv.
Step 2.1: Run virtualenv to check if it's now working...
Step 3: Still not working? Go to your prevouis console log to find where it says "WARNING: The script virtualenv.exe is installed in
'C:\Users\username\AppData\Roaming\Python\Python310\Scripts' which
is not on PATH."
Step 4: Copy the specified path from the warning message.
Step 5: Search for and open "System Properties" on your PC.
Step 6: Click the Advance tab, and then the Environment Variables Button on the bottom right.
Step 7: Click the variable value "Path" and then click edit.
Step 8: In the Edit Environment variable window, click new then paste your path in any slot.
Step 9: MAKE SURE you click OK twice and not to just exit out.
Step 10: Reboot terminal and check again.
Use py -m virtualenv Your_Folder_Name
To install to a specific folder e.g E:\publish
pip install virtualenv
virtualenv .
Try to run
PowerShell.exe -command "./venv/Scripts/activate"
You just need to reinstall virtualenv. First of all you need to uninstall virtualenv with this command.
pip uninstall virtualenv
Then just reinstall with this command.
pip install virtualenv
Solution-1: python -m venv name_of_virtual_environment
E:\code\python\tvenv>python -m venv myenv
E:\code\python\tvenv>cd myenv\Scripts\
E:\code\python\tvenv\myenv\Scripts>activate.bat
(myenv) E:\code\python\tvenv\myenv\Scripts>deactivate.bat
E:\code\python\tvenv\myenv\Scripts>
Solution-2: py -3 -m venv name_of_virtual_environment
E:\code\python\tvenv>py -3 -m venv myenv
E:\code\python\tvenv>cd myenv\Scripts
E:\code\python\tvenv\myenv\Scripts>activate.bat
(myenv) E:\code\python\tvenv\myenv\Scripts>deactivate.bat
E:\code\python\tvenv\myenv\Scripts>
For windows
First, install -> pip install virtualenvwrapper-win
Then setup -> mkvirtualenv myproject
Then you see the list of virtual environment
To see it you write-> lsvirtualenv
For working this environment we write -> workon myproject
I had this same issue using python3.
The solution was to use the python3 -m virtualenv . command.
This almost works for all
Open Command Prompt, navigate it to the Envs folder, run "env_name\Scripts\activate"
Check whether virtualenv is installed or not, if not install it:
pip install virtualenv
pip install virtualenvwrapper-win
Game On. Check on your IDE.
Try executing virtualenv.exe from its absolute path, like in my case i found it in C:\Users\<your user>\AppData\Roaming\Python\Python37\Scripts\virtualenv.exe.
I tried this and it worked, here refer the logs as follows:
Using base prefix c:\\users\\<user>\\appdata\\local\\programs\\python\\python37-32
New python executable in C:\somedir\dir2\dir3\ML_1\ml\env\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
I got this error too but I figure it out.
you just have to open PowerShell as administrator and then write following command Set-ExecutionPolicy unrestricted then type A. you are all set!
now uninstall the packages and re-install them.
Now if you write flask --version or virtualenv --version There will be no error at all.
Windows
If you want to use the lsvirtualenv command with virtualenv, follow the steps below.
Incorrect:
python -m pip install virtualenv
python -m pip install virtualenvwrapper
Correct:
python -m pip install virtualenv
python -m pip install virtualenvwrapper-win
Basic Uses
Then, to create a virtual environment:
mkvirtualenv youVirtualEnvironmentName
It will be activated automatically:
C:\Users\YourUserName (youVirtualEnvironmentName) λ
First, to access an existing virtual environment:
C:\Users\YourUserName λ workon youVirtualEnvironmentName
Next, to exit the currently active virtual environment:
C:\Users\YourUserName (youVirtualEnvironmentName) λ deactivate
Finally, to list all your virtual environments:
C:\Users\YourUserName λ lsvirtualenv
dir /b /ad 'C:\Users\YourUserName\Envs'
==================================================================
youVirtualEnvironmentName
Make sure that virtualenv has been installed correctly. Check in the python scripts subfolder - there must exist an .exe named virtualenv.exe. If not, uninstall will pip uninstall virtualenv and install again.
1)First Way as
python -m virtualenv name_of_virtual_environment
OR
2)Second Way as
py -3 -m venv name_of_virtual_environment
Open a cmd or ps with run as admin.
Now run pip uninstall virtual.
pip install virtual.
Done :D
Implementation:
Go to the directory where you want to make a python env.
type: virtualenv myEnv
beep bop boop done.
ps: Always use cmd or powershell with run as admin if you're installing some new package.

No module named win32com

I've just installed Python for the first time and I'm trying to reference the win32com module however, whenever I try to import it I get the message "no module name win32com".
Any ideas?
the below plus add pywin32 in PyCharm setting work for me
python -m pip install pywin32
As it is not built into Python, you will need to install it.
pip install pywin
Since win32com is a Windows-specific package, this answer will be geared towards Windows users.
Option 1: Install locally with pipenv (recommended)
You can use a package manager like pipenv to manage your dependencies.
Ensure you have pipenv installed (pip install pipenv).
In your project directory, run pipenv install pypiwin32 to install the package.
Now you can run your code using commands like the following pipenv run main.py
Example main.py code:
import win32com
print(win32com)
Option 2: Install locally with venv (recommended)
If pipenv isn't your thing, you can use the built-in virtual environments.
From your project directory, run python -m venv venv to setup you virtual environment.
Run venv\Scripts\activate.bat from your project directory whenever you want to use this virtual environment (you will see (venv) added to your shell prompt to know it's activated).
Run pip install pypiwin32 from your active virtual environment to install the package.
You can run your code like python main.py so long as the virtual environment is active.
Option 3: Install globally (typically not recommended)
This is not typically recommended, but included anyway.
Using pip install pypiwin32 you can install the package globally.
Then you can run your code with just python main.py.
This will work as well
python -m pip install pywin32
You should try using pip this way:
pip install pypiwin32
It is pypiwin32 which should work.
When working with python projects its always a good idea to create a so called virtual environment, this way your modules will be more organized and reduces the import errors.
for example lets assume that you have a script.py which imports multiple modules including pypiwin32.
here are the steps to solve your problem:
1. depending on you operating system you need to download and install virtualenv package, in debian its as simple as sudo apt install virtualenv .
2. after installing 'virtualenv' package go to your project/script folder and create a virtualenv folder with virtualenv venv it creates a folder named venv in that directory.
3. activate your virtualenv source /path/to/venv/bin/activate if your already in the directory where venv exists just issue source venv/bin/activate
4. after activating your venv install you project dependencies pip install pypiwin32 or pip install pywin
5. run your script, it wont throw that error again :)

Categories