Macport Portfiles do not work in Virtualenv - python

I have installed PyQt4 through Macport sudo port install py27-pyqt4 and virtualenv with pip install virtualenv. Whenever I run my PyQt programs while i am in the virtual environment I would receive the following error:
ImportError: No module named PyQt4.QtGui
However, I am able to run the same application when I am out of the virtual environment. What could be the reason for this issue and how do I resolve it?

Looks like you may have not installed PyQt4 into the virtual environment, usually the steps are as follows:
1) Create a virtual env: virtualenv ve_name
2) Activate the created virtualenv: source path_to_ve_name/bin/activate (at this point your shell will get ve_name prepended to it, and your $PATH will get updated, so whatever you install via pip will end up going into path_to_ve_name/bin)
3) Install all the dependencies while keeping ve_name active: pip install package-name, etc.
Once you have done this, you need to install PyQt4 into that virtual environment, there's an example here: How to install SIP and PyQt on a virtual environment? It looks like a simple pip install doesn't work with PyQT, so check out the suggestions in that question.
When you are done dealing with ve_name, you just need to deactivate your virtual environment (using command deactivate from your shell). This will revert your $PATH variable, and you can either create a new ve for a new project, or resume working on the same project by reactivating the created ve.

Related

Pip in pycharm install packages in conda env and not in active venv inside project

i'm having a weird problem...
I can install packages using the built-in package manager in pycharm. But for some reason everytime i use "pip install (xx)" it is installing the packages in a conda env somewhere on my mac...
How can i solve this ?
I've tried the following:
close --> reopen pycharm //
deactivate and activate the venv //
Checked project intepreter is the right one (which it is...)
You're inside the virtual environment venv, while being inside the Conda base environment (note the (venv) and (base) to the left of your prompt). Conda is likely overriding your venv's pip.
My bet as to why this is happening is that, during installation, you set Conda to autostart its base environment whenever a new terminal is open (be it inside PyCharm or not).
You can try to either:
exit Conda (with conda deactivate) and try pip install again (check to see that you're still inside the venv virtual environment).
install the packages directly from PyCharm's GUI - note the small + sign on the bottom-left of the package list. This won't solve the issue related to your terminal, but will function as a workaround for now.
Note that these aren't guaranteed to work, because you may have additional configurations on your system (either installed directly by you, or indirectly by Conda when you installed it).

Module installs but doesn't import

I'm on an OS Catalina and I'm trying to install and run Mephisto (see https://github.com/facebookresearch/mephisto/blob/master/docs/quickstart.md). I created a python3 virtual environment and then went to the directory and ran
sudo pip3 install -e .
This seems to have run fine as I can now run mephisto and see the list of commands and options. However when I run mephisto register mturk it throws No module named 'mephisto.core.argparse_parser' because of an import statement in the python file. This seems like a general issue of a module installing but not importing the module properly, but would appreciate help in how to fix it. Is it because my $PYTHONPATH is currently empty?
Mephisto Lead here! This seems to have been a case of unfortunate timing, as we were in the midst of a refactor there and some code got pushed to master that should've received more scrutiny. We'll be moving to stable releases via PyPI in the near future to prevent things like this!
I created a python3 virtual environment and then went to the directory and ran
sudo pip3 install -e .
You should not have used sudo to install this library, if you meant to install it in a virtual environment. By using sudo the library probably got installed in the global environment (not in the virtual environment).
Typically:
create a virtual environment:
python3 -m venv path/to/venv
install tools and libraries in this environment with:
path/to/venv/bin/python -m pip install Mephisto
use python in the virtual environment:
path/to/venv/bin/python -c 'import mephisto'
use a tool in the virtual environment:
path/to/venv/bin/mephisto
Is it because my $PYTHONPATH is currently empty?
Forget PYTHONPATH. Basically one should never have to modify this environment variable (this is almost always ill-informed advice to get PYTHONPATH involved).
Check the __init__.py file is in the module's file directory. If not try creating an empty one.

Installed modules not found by Python

I need to use the autogui module to do something in Python. When I run the script, it says that it can't find the autogui module so I installed it with
pip install autogui.
But when I run the script again it still says me this module doesn't exist.
Method 1:
You're probably having trouble setting up your correct Python Interpreter and working within it,try the following in VSCode.
Ctrl + Shift + p
And enter the following in the field.
python: select interpreter
Select the desired environment and reinstall PyAutoGui
Method 2:
Creating a virtual environment for your project where all your packages will be installed and will be isolated from others, and will have no import errors since it's an environment specifically for the project that you're working on.
I assume you use Windows, so open the command line in your working directory, or open your working directory in VSCode and enter the following in the command-line tool that is provided within VSCode.
The Python installers for Windows include pip. You should be able to access pip using:
py -m pip --version
You can make sure that pip is up-to-date by running the following
py -m pip install --upgrade pip
Installing virtual environment
py -m pip install --user virtualenv
Creating a virtual environment
py -m venv env
The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it env.
venv will create a virtual Python installation in the env folder.
Finally, to activate the environment run the following command
.\env\Scripts\activate
That will activate your environment.
pip install pyautogui
Make sure to change your interpreter to the one that you just created in the env/bin folder and run your code, or you could just enter the path to the *python file located in the env/bin folder.
Try pyautogui - I had the same problem. Instead of autogui, write `pyautogui. Or, if you are running python3 or higher then try:
pip3 install pyautogui.

How to stop python from installing modules in specific virtual environment and install them globally?

I was wondering How can I stop python from installing modules in specific virtual environment and install them globally;or probably virtaulenv won't let me do that!?
if you have (venv) before you terminal input, your virtual environment is activates. so you can using deactivate command for deactivating. in addition if you have a (base) you should be closing your anaconda base environment so you can see this answer
You simply install the package without activating your virtual environment. Once you install your package that way, it would be installed globally instead of inside a global environment.
Also, to confirm this, you can activate your virtual environment and run the command pip list , this will show all the packages in that virtual environment, and also try deactivating the virtual environment and run the command pip list, this would show all packages in the system.
I hope this answers your question
I believe I've found some sort of solution ,by just typing "pip install -target C:\Users\YOURNAME\AppData\Roaming\Python\Python37\site-packages"
it's kind of a lame solution but I think it's better than nothing!
By the way there was no pip.exe in my python folder!!!

pip-installed package inside conda virtual environment is not isolated

I am developing a simple python package (on macOS 10.14) and I have problems in setting the instructions how to install it. Right now the package is not available anywhere yet, but think of it as a dummy "hello world" package with a dummy "hello world" function inside a dummy "hello world" module. Of course it has a proper setup.py script that would allow users to install and uninstall the package with pip.
When I install and test it myself everything works fine, the problem is not related to the package itself.
The issue is that I cannot make conda virtual environments and pip work together... Next to my setup.py script there is a environment.yaml file that specifies the dependancies required for my package. Based on this file I create a virtual environment with:
conda env create --prefix ENV -f environment.yaml
I have conda 4.7.12 with Python 3.7.3 inside so the virtual environment has it's own pip. So I activate the virtual environment and explicitly call the pip inside to install my package in the virtual environment:
/Users/my_name/Desktop/dev_dir/ENV/bin/pip install . --user
The installation is successful and the package can be imported. However when I deactivate the virtual environment with conda deactivate and run python interpreter from the conda base environment (version 3.6.9) I can still load my package! For some reason it is available outside of that particular virtual environment...
Later, when I run the 'inner' pip from conda base shell:
/Users/my_name/Desktop/dev_dir/ENV/bin/pip uninstall pkg
The removal seems to go through as well. I get a message:
Uninstalling pkg-0.0.0.9000:
Would remove:
/Users/my_name/.local/lib/python3.7/site-packages/pkg-0.0.0.9000.dist-info/*
/Users/my_name/.local/lib/python3.7/site-packages/pkg/*
Proceed (y/n)? y
Successfully uninstalled pkg-0.0.0.9000
suggesting that the package was indeed installed in a directory .local, outside conda virtual environments.
And the best for the last: even after this uninstallation when I run python interpreters (regardless of which environment from) and I try to import pkg it still works! when I then type pkg in the interpreter I get the path to my development directory:
>>> import pkg
>>> pkg
<module 'pkg' from '/Users/my_name/Desktop/dev_dir/pkg/__init__.py'>
Could someone please help me disentangle this mess? I would like to have my package installed inside the virtual environment, nicely isolated. And also - it should be gone after uninstallation, right?
PS. PYTHONPATH variable is never set anywhere at any stage, I have been checking that...
when I then type pkg in the interpreter I get the path to my development directory
This can only happen if:
You modified your PYTHONPATH to include /Users/my_name/Desktop/dev_dir which you didn't do
You are running the interpreter while you are in the folder /Users/my_name/Desktop/dev_dir, seems likely as you called it your development folder.
Check the output of print(sys.path), which lists all directories that are searched when doing import (standard locations + PYTHONPATH) and also print(os.getcwd()) as the current working directory is also searched
You tried installing your package to your activated conda environment using
/Users/my_name/Desktop/dev_dir/ENV/bin/pip install . --user
Looking at [the docs](https://pip.pypa.io/en/stable/reference/pip_install/#cmdoption-user] however:
--user
Install to the Python user install directory for your platform. Typically ~/.local/
So the --user option is messing with your intention to install into the currently active environment. But pip actually does that by default when run inside a virtual environment. So simply do:
conda activate <your envname>
pip install .
#FlyingTeller already correctly identified the issue. I just wanted to point out that you could further streamline your process by adding the installation for your package into your YAML definition. For example,
name: my_env
channels:
- defaults
dependencies:
- python=3.7.3
- pip
- pip:
- -e /Users/my_name/Desktop/dev_dir/pkg
This is also further in line with the best practices (see "Using Pip in a Conda Environment").
Just wanted to hopefully clear some up by telling you this keeps happen to many and if you forget the rule that is NO root install with conda, all rules for your files might change and suddenly it keeps asking for sudo AND fails. Conda = NO SUDO! Hope you got it fixed!
You have to add the pip package to your environment (see https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html), otherwise the packages will be installed by the global pip installation such that those packages can be accessed by all environments.
Therefore, create an environment using
conda create --name exampleenv pip
instead of
conda create --name exampleenv

Categories

Resources