scripting python install library with pip on virtualenv - python

I'd like to use virtualenv in order to setup my environment and to install specific libraries.
I want to script the whole process, but so far, it's not working.
Here is my attempt:
import subprocess
import pip
virtualenv_dir="my_directory"
subprocess.call(["virtualenv", virtualenv_dir, "--system-site-packages"])
activate_this_file="{}/bin/activate_this.py".format(virtualenv_dir)
# instead of sourcing the /bin/activate file, I update dynamically
# my current python environment
execfile(activate_this_file, dict(__file__ = activate_this_file))
pip.main(["install","my_lib"])
This way, my_lib is installed on /usr/lib/python2.7/site-packages instead of "my_directory/lib/python2.7/site-packages", as I wish.

I've came up with the following work-around:
# In main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import subprocess, os
virtualenv_dir="my_directory"
subprocess.call(["virtualenv", virtualenv_dir, "--system-site-packages"])
subprocess.call([os.path.join(virtualenv_dir, 'bin/python'),"-c","import pip; pip.main(['install','my_lib'])])

Related

python3 - ImportError: No module named twython

I am using python3.
I installed twython on my MAC using pip3 command and I confirmed it was successfully installed.
When I run my python file, it comes up with:
ImportError : No module named twython
My code is as follows:
import sys
import string
import json as simplejson
from twython import Twython
I can't comment the response from #ajxs, but as additional information to his repsonse:
You can change the default python interpreter like this in your MAC terminal:
nano ~/.bash_profile
Add this to the file:
alias python=python3
exit the the bashrc file and run the following command:
source ~/.bash_profile
Now you can check the defaul python version with:
python --version
Maybe this helps you.
First thing that springs to mind is to check that you're running the script with the correct version of Python. Use python --version on the command line to check which version of Python you're executing by default. I've definitely had problems like this before when I've forgotten that my system's default version was 2.7 and I needed to use python3 to run Python 3 on the command line.

Python - ModuleNotFound error using Anaconda

I am trying to run this program:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from io import open
from multiprocessing import Pool
import buildingspy.simulate.Simulator as si
# Function to set common parameters and to run the simulation
def simulateCase(s):
''' Set common parameters and run a simulation.
:param s: A simulator object.
'''
s.setStopTime(86400)
# Kill the process if it does not finish in 1 minute
s.setTimeOut(60)
s.showProgressBar(False)
s.printModelAndTime()
s.simulate()
def main():
''' Main method that configures and runs all simulations
'''
import shutil
# Build list of cases to run
li = []
# First model
model = 'Buildings.Controls.Continuous.Examples.PIDHysteresis'
s = si.Simulator(model, 'dymola', 'case1')
s.addParameters({'con.eOn': 0.1})
li.append(s)
# second model
s = si.Simulator(model, 'dymola', 'case2')
s.addParameters({'con.eOn': 1})
li.append(s)
# Run all cases in parallel
po = Pool()
po.map(simulateCase, li)
# Clean up
shutil.rmtree('case1')
shutil.rmtree('case2')
# Main function
if __name__ == '__main__':
main()
and I keep getting this error:
File "C:/Users/Toshiba/.spyder-py3/temp.py", line 11, in <module>
import buildingspy.simulate.Simulator as si
ModuleNotFoundError: No module named 'buildingspy'
I already installed the package using pip more than one time and nothing changes.
What am I missing?
This is the source of this code.
That error might be due to having multiple python installations on your computer:
https://docs.python.org/3/installing/#work-with-multiple-versions-of-python-installed-in-parallel
Please add the following lines somewhere to your script (or to a new script) and run it once from Spyder, and once from the console and compare the output:
import sys
print("python: {}".format(sys.version))
# also add the following if running from python 3
from shutil import which
print(which("python"))
Buildingspy has to be installed using pip, I would recommend to install it using the command:
python -m pip install -U https://github.com/lbl-srg/BuildingsPy/archive/master.zip
Anaconda adds an Anaconda prompt to the Start menu, use that to make sure the path to python.exe is correct.
Once BuildingsPy is installed correctly, you will run into the problem that on Windows multiprocessing will not work from Spyder (or, from IPython/Jupyter), please also read this issue:
https://github.com/lbl-srg/BuildingsPy/issues/179
You will have to run your script from the command line.

Python 3.x - How to get the directory where user installed package

Suppose that I am building a Python package with the following folder tree:
main
|-- setup.py
|-- the_package
|--globar_vars.py
|--the_main_script.py
When the user performs anyway to install it, like:
sudo python setup.py install
python setup.py install --user
python setup.py install --prefix=/usr/local
Or well, using PIP:
pip install 'SomeProject'
I want that the folder where the package was installed be saved on the global_vars.py, in any variable, Eg:
globarl_vars.py
#!/usr/bin/env python3
user_installed_pkg = '/some/path/where/the/package/was/installed'
There is someway to get it? Thanks in advance.
Assuming your SomeProject package is a well-formed Python package (which can be done using setuptools), a user can derive the location simply use the pkg_resources module provided by setuptools package to get this information. For example:
>>> from pkg_resources import working_set
>>> from pkg_resources import Requirement
>>> working_set.find(Requirement.parse('requests'))
requests 2.2.1 (/usr/lib/python3/dist-packages)
>>> working_set.find(Requirement.parse('requests')).location
'/usr/lib/python3/dist-packages'
However, the path returned could be something inside an egg which means it will not be a path directly usable through standard filesystem tools. You generally want to use the Resource manager API to access resources in those cases.
>>> import pkg_resources
>>> api_src = pkg_resources.resource_string('requests', 'api.py')
>>> api_src[:25]
b'# -*- coding: utf-8 -*-\n\n'
import wtv_module
print(wtv_module.__file__)
import os
print(os.path.dirname(wtv_module.__file__))

Python run package without installing

As part of my build system, I am using a modified version of a Python package (cogapp). I don't want to install the package because:
I've modified the package and don't want to worry about collision with unmodified versions which may already be installed.
It's nicer if the users of the build system don't need to install extra packages.
However, I'm having problems with using the package if it's not installed. If it is installed, I can run:
python -m cogapp <additional args>
and everything runs as intended.
The package has a __main__.py script:
import sys
from cogapp import Cog
sys.exit(Cog().main(sys.argv))
I tried running this directly, e.g.:
python -m <path>/__main__ <additional_args>
But I get the error:
...
/__main__.py", line 3, in <module>
from cogapp import Cog
ImportError: No module named cogapp
This is probably related to the error I get if I run __init__.py:
from .cogapp import *
The error is:
from .cogapp import *
ValueError: Attempted relative import in non-package
How can I run the package as a package?
EDIT:
I found a fix by removing all the relative imports from cogapp, and removing the -m, i.e. not running as a module. In this instance it's not too bad because it's a small package with only a single directory. However I'm interested in how this should be done in future. There's lots of stuff written around this subject, but no clear answers!
Here's the solution I've come to.
Disclaimer: you are actually installing the package but to a different path than the standard one.
$ mkdir newhome
$ python setup.py install --home=./newhome
$ PYTHONPATH=$PWD/newhome/lib/python <COMMAND_NEEDING_THAT_PACKAGE>

Python: installing multiprocessing

I need to import the multiprocessing module in Python 2.5.
I've followed the instructions here exactly: http://code.google.com/p/python-multiprocessing/wiki/Install
make and make test run without errors. I've also edited $PYTHONPATH to include the directory where the package is installed.
But 'import multiprocessing' still says: "ImportError: no module named multiprocessing".
What am I doing wrong? Is there some step missing from these instructions? I haven't installed a Python module before.
Navigate to the directory containing the package then type:
python setup.py install
This info was contained in the INSTALL.txt file.
http://code.google.com/p/python-multiprocessing/source/browse/trunk/INSTALL.txt
perhaps you can try:
import sys
sys.path.append('/path/to/processingdotpylibs/')
import processing

Categories

Resources